Simple C Program to save details to a text file (student records)

Reading Time: 4 minutes

Today we are going to take a look at how can we create a simple database program using a very simple method with C programming. We are just using a .txt file as our backend. It’s not the most secure database you’ll ever find but should be good enough for you to get a glimpse of file writing and reading with C. So, let’s get on to our simple C program to save details to a text file.

Full Code

Here’s the full source code for this project.

#include <stdio.h>
#include <string.h>

int main() {
    int tmp_loop_count = 0; //a temporary count to keep track in while loop
    int number_of_students = 0; //stores input by the user for number of students to save
    char student_name[50]; //stores input by the user for name of each student, in each while loop iteration
    char student_age[50]; //stores input by the user for age of each student, in each while loop iteration
    char student_gender[50]; //stores input by the user for gender of each student, in each while loop iteration
    char student_id[50]; //stores input by the user for student id of each student, in each while loop iteration
    char student_phone[50]; //stores input by the user for phone number of each student, in each while loop iteration
    FILE *fptr; //file pointer which deals with opening & writing to a file
    char filename[50]; //stores name for the file to save student details in.

    strcpy(filename, "../student_details.txt"); //assigns the string "../student_details.txt" to filename variable
    fptr = fopen(filename, "w"); //open the file with filename & assign that file to fptr.

    printf("Enter number of Students to Save : ");
    scanf("%d", &number_of_students);

    getchar();
    while(tmp_loop_count < number_of_students) {
        //get student details from input.
        //gets used to get student names which contain spaces also. (scanf only get until meet first space)
        printf("\nEnter Student Name : ");
        gets(student_name);

        printf("\nEnter Student Gender : ");
        gets(student_gender);

        printf("\nEnter Student Age : ");
        gets(student_age);

        printf("\nEnter Student ID Number : ");
        gets(student_id);

        printf("\nEnter Student Phone Number : ");
        gets(student_phone);

        //write student details to file.
        fprintf(fptr, "%s,%s,%s,%s,%s\n", student_id, student_name, student_gender, student_age, student_phone);
        tmp_loop_count++;
    }
}

What are we doing here again?

Here’s what are we going to achieve in this project.

  • Enter the number of students (say n) to save to the console
  • Enter the details of those n number of students to the console
  • After entering details of each student, automatically save those details to a student_details.txt text file

let’s explore the lines

Lines 1 and 2
#include <stdio.h>
#include <string.h>

These are the preprocessor directives that we’ve used.

The stdio.h file contains library functions such as printf() and scanf(). So in order to use those functions, we need to include that file in our program by using #include directive.

The string.h file contains library functions such as strcpy(). So in order to use those functions, we need to include that file in our program by using #include directive.

Lines 5 to 13
int tmp_loop_count = 0;
int number_of_students = 0;
char student_name[50];
char student_age[50];
char student_gender[50];
char student_id[50];
char student_phone[50];
FILE *fptr;
char filename[50];

These lines contain the variable declarations. We have initialized only the tmp_loop_count variable and number_of_students variable to value 0.

Other variables are automatically initialized to random values called as the garbage values.

Lines 15 and 16
strcpy(filename, "../student_details.txt");
fptr = fopen(filename, "w");

This is the part where we create the student_details.txt file if it not exist, or overwrite the file with that name.

The ../ is used in the file path, to go back in the folder structure. We have used two dots (..) before the forward slash (/) to tell the program to go the previous folder. If we used only one dot and the forward slash (./) it refers to the same folder as the executing file.

Lines 22 to 42
while(tmp_loop_count < number_of_students) {
	printf("\nEnter Student Name : ");
	gets(student_name);

	printf("\nEnter Student Gender : ");
	gets(student_gender);

	printf("\nEnter Student Age : ");
	gets(student_age);

	printf("\nEnter Student ID Number : ");
	gets(student_id);

	printf("\nEnter Student Phone Number : ");
	gets(student_phone);

	fprintf(fptr, "%s,%s,%s,%s,%s\n", student_id, student_name, student_gender, student_age, student_phone);
	tmp_loop_count++;
}

The while loop executes until the number of students to be saved is completed.

One iteration of the while loop corresponds to one student. So, during one iteration, the programs asks for student details like name, gender….

The line 40 writes to the student_details.txt file, the values passed in as parameters within the brackets, in order.

Conclusion

So that’s our simple C program to save details to a text file while keeping the different fields like first name, last name, age… separately. We’ll learn how to retrieve these saved details from the .txt file next time.

If this article is hard for you, I suggest you checkout this article which would explain some simple things not covered in here.

5 1 vote
Article Rating

Leave a comment. We love your feedback.

29 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Nimasha
Nimasha
4 months ago

Even though you have mentioned as simple, really priceless.

Vidushika Tharuni
Vidushika Tharuni
4 months ago

Good job๐Ÿ‘๐Ÿป

Zenith
Zenith
4 months ago

It would be great if you and put some more comments before variables and function to make the code much more explicit and comprehensive for people who are reading this with a wee knowledge in C .The graphics are great,attractive and it is obviously a great job

Lakshitha S Kumara
Lakshitha S Kumara
4 months ago

Great work..๐Ÿคฉ

Sachini Buddhika
Sachini Buddhika
4 months ago

Great work!go ahead!

Imasha Athukorala
Imasha Athukorala
4 months ago

Very useful …great .

Imesha Basnayake
Imesha Basnayake
4 months ago

It’s really worthโœŒ๏ธ #keepitup ๐Ÿ‘

Bhanuka Uyanage
Bhanuka Uyanage
4 months ago

Great job machan๐Ÿ˜.You are a good writter and explainer.Multi talented.๐Ÿ”ฅ

Janith Herath
Janith Herath
4 months ago

Great work

Pulini Tilanka
Pulini Tilanka
4 months ago

Superb ๐Ÿ‘Œ

Mithusha S
Mithusha S
4 months ago

Nice work Rukshan ๐Ÿคฉ
Keep going ๐Ÿ‘๐Ÿฝ

Kushan
Kushan
4 months ago

Great work rukshan….keep it up…This will be really helpful for students who are going to learn c programming….

Sarjoon Ibnu Nisvar
Sarjoon Ibnu Nisvar
4 months ago

Rukshan.
This a great beginning man. This is definitely not so simple.
Best of luck.

Wanuja Ranasinghe
Wanuja Ranasinghe
4 months ago

Nice work Rukshan!!!

29
0
Would love your thoughts, please comment.x
()
x