This week we explore file input/output (I/O) and modular program design in C.
You will learn to read and write files, store structured data, and create a simple management system that persists between runs.
Create a C program week5_task1_file_io.c that:
- Opens a text file for writing (e.g.,
"data.txt"). - Writes a few lines of text into it using
fprintf(). - Closes the file properly.
- Opens the same file again for reading.
- Reads its contents line by line using
fgets()and prints them to the console.
Use the provided stub file:
week5_task1_file_io.c- Always check that the file pointer returned by
fopen()is notNULL. - Use clear messages to indicate whether you are writing or reading.
- Close every opened file using
fclose(). - Avoid hardcoding long text — optionally read input from the user.
- Ensure your program compiles without warnings using
-Wall.
$ ./week5_task1_file_io
Writing lines to data.txt...
Reading contents:
Hello, file I/O in C!
This is another line.Create a C program week5_task2_struct_save_load.c that:
- Defines a
struct Studentcontaining a name, age, and GPA. - Saves one student record to a text file using a helper function
save_student(). - Loads that record from the file using another function
load_student(). - Prints the loaded record to confirm that it matches the original.
Use the provided stub file:
week5_task2_struct_save_load.c- Implement both
save_student()andload_student()with proper file checking. - Save the data in a simple text format:
name age gpa. - Use
fprintf()andfscanf()for writing and reading. - Avoid using global variables.
- Handle cases when the file cannot be opened.
$ ./week5_task2_struct_save_load
Saving student to file...
Loading student from file...
Loaded student: Alice, 21, GPA 3.75Create a C program week5_task3_student_management_system.c that:
- Allows listing, adding, and saving students.
- Uses an array of
struct Studentto store data in memory. - Loads existing data from a file at startup.
- Saves all students back to the file before exiting.
- Displays a simple text-based menu with options (list/add/save-exit).
Use the provided stub file:
week5_task3_student_management_system.c- Implement all functions marked
TODOin the stub:load_students()save_students()add_student()list_students()
- Use text file storage (
students.txt) and one line per record. - Ensure program handles empty files gracefully.
- Maintain clean and readable code with comments.
- Compile without warnings using the provided Makefile.
=== Student Management System ===
1. List students
2. Add student
3. Save and Exit
Select an option: 2
Enter name: Alice
Enter ID: 101
Enter GPA: 3.8
Student added successfully!
=== Student Management System ===
1. List students
2. Add student
3. Save and Exit
Select an option: 3
Data saved. Goodbye!Extend Task 2 to save and load struct Student records in binary format using fwrite() and fread().
Create a new file:
week5_task4_struct_binary_io.cYour program should:
- Define the same
struct Studentas in Task 2. - Save the struct directly in binary format.
- Load the struct back and print its contents.
- Compare file sizes between text and binary versions.
- Use modes
"wb"(write binary) and"rb"(read binary). - Use
fwrite(&student, sizeof(Student), 1, fp); - Use
fread(&student, sizeof(Student), 1, fp); - Print confirmation messages during save and load.
- Check file pointer validity before any file operations.
$ ./week5_task4_struct_binary_io
Saving student in binary format...
Loading student from binary file...
Loaded student: Alice, 21, GPA 3.75
Binary file size: 60 bytesYou can compile all tasks with:
makeRun individual programs with:
make run1
make run2
make run3Clean all executables:
make clean- GNU C Library – File Streams
https://www.gnu.org/software/libc/manual/html_node/Streams.html - ISO C
stdio.hReference (cppreference)
https://en.cppreference.com/w/c/io
- Learn C – Structs and File Handling (Programiz)
https://www.programiz.com/c-programming/c-structures - Separate Compilation and Headers (GeeksforGeeks)
https://www.geeksforgeeks.org/separate-compilation-and-header-files-in-c/
- C Binary File I/O (TutorialsPoint)
https://www.tutorialspoint.com/cprogramming/c_file_io.htm - Binary I/O Examples (cplusplus.com)
https://cplusplus.com/reference/cstdio/fwrite/
After completing Week 5, you will be able to:
- Read and write data to text and binary files in C.
- Persist and reload structured records using
struct. - Implement a small modular project with file persistence.
- Understand how to separate code into multiple functions and files.
- Compare text and binary file storage formats.
Students can submit their completed Week 5 work in one or BOTH of the following ways:
If you have been working in GitHub Codespaces or using your forked course repository:
- Ensure all your Week 5 source files are committed and pushed to your repository.
- Include the following files in your repository under the
src/folder (or equivalent):week5_task1_file_io.cweek5_task2_struct_save_load.cweek5_task3_student_management_system.c- (optional)
week5_task4_struct_binary_io.c
- Provide a URL link to your GitHub repository in the Moodle submission field.
- Make sure your repository is public or accessible to the instructor.
If you prefer, you may submit the actual source code files directly:
- Upload your
.cfiles for all tasks as separate attachments. - Each file must include:
- A comment at the top with your Name, Surname, and Student ID.
- Optional: a link to your GitHub repository.
- Compilation instructions if special flags are required.
- Check that each program compiles without errors or warnings using:
make lab5