Decode School

Decode School - The School for Problem Solvers

Welcome to my blog. Subscribe and get my latest blog post in your inbox.

Write something into the file

Earse the content of file sample.txt and write something new.

Program or Solution

					    
							
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fptr;
    char ch;
    char content[1000];
   
    
    fptr = fopen("sample.txt", "w");
    if (fptr == NULL)
    {
       printf("Cannot open file \n");
       return 0;
    }
    scanf("%[^\t]s",content);
    fprintf(fptr,"%s",content);
    fclose(fptr);
    return 0;
}
					    
					

Program Explanation

fopen() to open the specified file.

"sample.txt" is file name.

"w" denotes open it in write mode and earse the existing content.

So write operation starts from begining.

Fprintf() writes the content into the file.

Note: sample.txt should exist in the same folder where your program is located.