Introduction to Artificial Intelligence and History of AI
APPENEDING OF DATA TO AN EXISTING FILES.
1. PROGRAMMING FOR PROBLEM SOLVING
TOPIC: APPENDING DATA TO EXISTING FILES
NAME:- ANUSHA ASHRAF
H.NO:- 23L51A7205
ROLL.NO:- 05
BRANCH:- AI&DS
2. A File handing in C is the process in which
we create, open, read, write, and close
operations on a file.
C language provides different functions
such as fopen(), fwrite(), fread(), fseek(),
fprintf(), etc. to perform input, output, and
many different C file operations in our
program.
FILE HANDLING
3. APPENDING DATA TO EXISTING
FILE
Appending data in C programming means
adding new data to the end of an existing
file without overwriting the existing data.
This is useful when you want to add new
data to a file without losing the old data. To
append data to a file in C programming, you
can use the fopen() function with
the "a" mode flag.
4. C PROGRAM USING
APPENDING DATA TO
EXISTING FILE
The program opens the given
file and asks the user to enter
the data to append to the file
and successfully appends it to
the existing file. It also gives a
feedback on the success of the
operation.
#include <stdio.h>
int main()
{
FILE *fp;
char data[100];
fp = fopen(“anusha.c”, “a”);
printf(“Enter data to append:n”);
fgets(data, sizeof(data), stdin);
fprintf(fp, “%s”, data);
printf(“Data appended successfullyn”);
fclose(fp);
return 0;
}
Output:-
Enter data to append:
Good
Data appended successfully
5. PROGRAM EXPLANATION
#include <stdio.h>
int main()
{
FILE *fp;
char data[100];
fp = fopen(“anusha.c”, “a”);
printf(“Enter data to append:n”);
fgets(data, sizeof(data), stdin);
fprintf(fp, “%s”, data);
printf(“Data appended successfullyn”);
fclose(fp);
return 0;
}
Output:-
Enter data to append:
Good
Data appended successfully
1.Header Inclusion:
•The program includes the standard input/output library (<stdio.h>) for
input/output operations.
2.Variable Declaration:
•FILE *fp;: Declares a pointer to a file (FILE) named fp.
•char data[100];: Declares an array named data to store user input.
3.File Opening:
•fp = fopen(“anusha.c", "a");: Opens the file named “anusha.c" in append mode
("a"). The file is opened for writing, and if it does not exist, it will be created.
4.User Input:
•printf("Enter data to append:n");: Prompts the user to enter data.
•fgets(data, sizeof(data), stdin);: Uses fgets to get input from the user, including
spaces. The entered data is stored in the data array.
5.Data Appending:
•fprintf(fp, "%s", data);: Appends the data stored in the data array to the file
using fprintf.
6.Output Confirmation:
•printf("Data appended successfullyn");: Informs the user that the data has
been appended successfully.
7.File Closing:
•fclose(fp);: Closes the file.
8.Return Statement:
•return 0;: Indicates successful execution of the program.
This program allows the user to enter data, appends it to the "file.txt" file, and provides
feedback on the success of the operation.