Append Something to the file

write something new into file sample.txt without earsing the exisiting content.

Try your Solution

Strongly recommended to Solve it on your own, Don't directly go to the solution given below.

#include<stdio.h> int main() { //write your code here }

Program or Solution

				
			
					
#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fptr;
    char ch;
    char content[1000];
   
    
    fptr = fopen("sample.txt", "a");
    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.

"a" denotes open it in write mode and keep existing content.

So write starts from last.

Fprintf() writes the content into the file.

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

Comments


Related Programs