Write a program in C to append lines at the end of a text file.File Name: input1.txtInput format :The input consists of the contents to be appended in the given file.Output format :The output displays the contents in the file after appending user input.Sample test cases :Input 1 :welcome to C programmingOutput 1 :Coding challenges are tests
Question
Write a program in C to append lines at the end of a text file.File Name: input1.txtInput format :The input consists of the contents to be appended in the given file.Output format :The output displays the contents in the file after appending user input.Sample test cases :Input 1 :welcome to C programmingOutput 1 :Coding challenges are tests
Solution
Here is a simple C program that appends text to the end of a file named "input1.txt".
#include <stdio.h>
int main() {
FILE *file;
char text[100];
// Open the file in append mode
file = fopen("input1.txt", "a");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}
printf("Enter text to append: ");
fgets(text, sizeof(text), stdin);
// Write the input text to the file
fputs(text, file);
// Close the file
fclose(file);
printf("Text appended successfully.\n");
return 0;
}
This program will ask the user to enter the text to be appended to the file. It then opens the file in append mode, writes the user's input to the file, and finally closes the file. If the file does not exist, it will be created. If it does exist, the text will be added to the end of the file, without deleting any existing content.
Similar Questions
Write C program to open an existing file“My.TXT” and read its content and displayit.
Which of the following operations can be performed on the file "NOTES.TXT" using the below code? FILE *fp; fp = fopen("NOTES.TXT", "r+"); ans. Writing Reading Read and Write Appending
To open a file c:\scores.txt for appending data, we use ____________
What will be the output of the following code? #include <stdio.h> int main() { FILE *file = fopen("test.txt", "w"); fputs("Line 1\nLine 2", file); fclose(file); file = fopen("test.txt", "r"); char buffer[50]; while (fgets(buffer, sizeof(buffer), file)) { printf("%s", buffer); } fclose(file); return 0; }Question 4AnswerA.Line 1 Line 2B.Line 1Line 2C.Compilation errorD.Runtime error
How do you append data to an existing file in Python?Question 8Answera.open(filename, 'b')b.open(filename, 'w')c.open(filename, 'a')d.open(filename, 'r')
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.