C Tutorial
C programming language provides the built-in functions for file processings.
We can write the file using any of the following file modes based on the requirements,
w -> used to open file for writing and new file is created always and assumed that file does not exists.
a -> used to open file for appending and initial position is at the end of the file.
r+ -> used to open file for update (both reading and writing), initial position is at the start of the file for reading and writing.
w+ -> used to open file for update (both reading and writing) and new file is created if file does not exists, initial position is at the start of the file for reading and writing
a+ -> used to open file for update (both reading and writing), initial position is at the end of the file for reading and writing.
This c program is used to write and create the text file based on user input student id, name and mark using file write mode.
student_id, student_name and mark are the user inputs and creates the file 'student_mark_list.txt' to store the student mark detail.
#include<stdio.h> void main() { FILE *fp; char *student_name; int student_id, mark; fp = fopen("student_mark_list.txt", "w"); printf("\nEnter student id: "); scanf("%d", &student_id); printf("\nEnter student name: "); scanf(" %s", student_name); printf("\nEnter student mark: "); scanf("%d", &mark); fprintf(fp, "%d\t%s\t%d\n", student_id, student_name, mark); fclose(fp); printf("\nStudent mark is written successfully!"); }
Output:
$ cc file-write.c $ ./a.out Enter student id: 23 Enter student name: student1 Enter student mark: 87 Student mark is written successfully!
created text file:
$ cat student_mark_list.txt 23 student1 87
If we run one more time with different inputs then again new file will be created with new inputs and old student mark detail will be lost.
$ ./a.out Enter student id: 32 Enter student name: student2 Enter student mark: 34 Student mark is written successfully!
created text file:
$ cat student_mark_list.txt 32 student2 34
It's possible to have the different format for each record in file if we want.
Lets see the above program to write file in comma separated file format instead of \t space
#include<stdio.h> void main() { FILE *fp; char *student_name; int student_id, mark; fp = fopen("student_mark_list.txt", "w"); printf("\nEnter student id: "); scanf("%d", &student_id); printf("\nEnter student name: "); scanf(" %s", student_name); printf("\nEnter student mark: "); scanf("%d", &mark); fprintf(fp, "%d,%s,%d\n", student_id, student_name, mark); fclose(fp); printf("\nStudent mark is written successfully!"); }
Output:
$ cc file-write.c $ ./a.out Enter student id: 35 Enter student name: student1 Enter student mark: 90 Student mark is written successfully!
created text file:
$ cat student_mark_list.txt 35,student1,90
If we are frequently adding data in a text file, then need to open a file in append mode to write the text file.
append mode supports both reading and writing, initial position is at the end of file.
#include<stdio.h> void main() { FILE *fp; char *student_name; int student_id, mark; fp = fopen("student_mark_list.txt", "a"); printf("\nEnter student id: "); scanf("%d", &student_id); printf("\nEnter student name: "); scanf(" %s", student_name); printf("\nEnter student mark: "); scanf("%d", &mark); fprintf(fp, "%d,%s,%d\n", student_id, student_name, mark); fclose(fp); printf("\nStudent mark is written successfully!"); }
Output:
$ cc file-write.c $ ./a.out Enter student id: 21 Enter student name: student1 Enter student mark: 98 Student mark is written successfully!
created text file:
$ cat student_mark_list.txt 21,student1,98
run again to add one more record to check whether appending or not.
$ ./a.out Enter student id: 32 Enter student name: student2 Enter student mark: 78 Student mark is written successfully!
created text file:
$ cat student_mark_list.txt 21,student1,98 32,student2,78
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page