Intro To Programming Week 9
Intro To Programming Week 9
Contents
1 Introduction to File Handling 2
1.1 Types of Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 File Handling Functions . . . . . . . . . . . . . . . . . . . . . . . 2
6 Conclusion 10
1
1 Introduction to File Handling
File handling is an essential part of programming that allows programs to read
data from files and write data to files. Files are used to store data permanently,
making it possible to retain information even after the program terminates.
3 int main () {
4 FILE * file ;
5 file = fopen (" example . txt " , " r ") ;
6 if ( file == NULL ) {
7 printf (" Error opening file .\ n ") ;
2
8 return 1;
9 }
10 // Perform file operations
11 fclose ( file ) ;
12 return 0;
13 }
3 int main () {
4 FILE * file ;
5 char ch ;
6
18 fclose ( file ) ;
19 return 0;
20 }
3 int main () {
4 FILE * file ;
5 char buffer [100];
6
3
14 while ( fgets ( buffer , 100 , file ) != NULL ) {
15 printf ("% s " , buffer ) ;
16 }
17
18 fclose ( file ) ;
19 return 0;
20 }
3 int main () {
4 FILE * file ;
5 char ch = ’A ’;
6
16 fclose ( file ) ;
17 return 0;
18 }
3 int main () {
4 FILE * file ;
5 char buffer [] = " Hello , World !";
6
4
15
16 fclose ( file ) ;
17 return 0;
18 }
3 int main () {
4 FILE * file ;
5 file = fopen (" example . txt " , " r ") ;
6 if ( file == NULL ) {
7 printf (" Error opening file .\ n ") ;
8 return 1;
9 }
10
3 int main () {
4 FILE * file ;
5 file = fopen (" example . txt " , " r ") ;
6 if ( file == NULL ) {
7 printf (" Error opening file .\ n ") ;
8 return 1;
9 }
10
13 fclose ( file ) ;
5
14 return 0;
15 }
3 int main () {
4 FILE * file ;
5 file = fopen (" example . txt " , " r ") ;
6 if ( file == NULL ) {
7 perror (" Error opening file ") ;
8 return 1;
9 }
10
16 fclose ( file ) ;
17 return 0;
18 }
3 int main () {
4 FILE * file ;
5 file = fopen (" example . txt " , " w ") ;
6 if ( file == NULL ) {
7 perror (" Error opening file ") ;
8 return 1;
9 }
10
15 fclose ( file ) ;
16 return 0;
17 }
6
4 Binary File Handling
Binary files store data in a binary format, which is not directly readable by
humans. Binary file operations use the same functions as text file operations
but handle data differently.
3 int main () {
4 FILE * file ;
5 int data = 12345;
6
16 fclose ( file ) ;
17 return 0;
18 }
3 int main () {
4 FILE * file ;
5 int data ;
6
7
14 fread (& data , sizeof ( int ) , 1 , file ) ;
15 printf (" Data read from file : % d \ n " , data ) ;
16
17 fclose ( file ) ;
18 return 0;
19 }
8
30 printf (" Enter age : ") ;
31 scanf ("% d " , & newStudent . age ) ;
32
9
72 for ( int i = 0; i < student_count ; i ++) {
73 printf (" ID : % d \ n " , students [ i ]. id ) ;
74 printf (" Name : % s \ n " , students [ i ]. name ) ;
75 printf (" Age : % d \ n \ n " , students [ i ]. age ) ;
76 }
77 }
78 }
79
92 switch ( choice ) {
93 case 1:
94 addStudent () ;
95 break ;
96 case 2:
97 deleteStudent () ;
98 break ;
99 case 3:
100 printStudents () ;
101 break ;
102 case 4:
103 exit (0) ;
104 default :
105 printf (" Invalid choice . Please try
again .\ n ") ;
106 }
107 }
108 return 0;
109 }
6 Conclusion
File handling in C is an essential skill for managing data storage and retrieval.
Understanding how to open, read, write, and close files, as well as handle errors,
10
is crucial for writing robust programs. Additionally, managing binary files and
implementing a menu-driven program for student records demonstrates practical
applications of these concepts.
11