File Handling
File Handling
Agenda:
What is a Stream
Opening and Closing of Files
Writing and Reading in Text Format
Writing and Reading in Binary Format
What is Stream
Categories of Streams:
Character / Text Stream
Binary Stream
Text Streams
In a stream of text, one unit of data is one character i.e. at a time we read or write a character or an array of
characters. Here we should know the type of encoding before we can read characters from text stream. Based on
the type of encoding used number of bytes a character takes is decided and this remains fixed throughout the
stream.
S A N D E E P
01010011 01000001 01001110 01000100 01000101 01000101 01010000
Binary Streams
In a stream of Binary, the unit of data is not fixed as it can be of any date type. The order of writing to the stream
must be known so that in the same order we can read data from the stream.
Short(2) Integer(4) Float(4) char(1) Char(1)
10101000111010101 10100110101010101010011010101010 10100110101010101010011010101010 01000110 1010011
Files allows us to store information permanently and access, alter that information whenever necessary
C Programming Language File Handling
Mode Description
Opens a text file for writing, if it does not exist then a new file is created. Here your program will start
w
writing content from the beginning of the file.
Opens a text file for writing in appending mode, if it does not exist then a new file is created. Here your
a
program will start appending content in the existing file content.
Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise
w+
create the file if it does not exist.
Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start
a+
from the beginning but writing can only be appended.
Closing a File
int fclose( FILE *fp );
The fclose( ) function returns zero on success, or EOF if there is an error in closing the file
This function actually, flushes any data still pending in the buffer to the file, closes the file, and releases any
memory used for the file.
C Programming Language File Handling
Note: You can also use int fscanf(FILE *fp, const char *format, ...) function to read strings from a file but it stops
reading after the first space character encounters.
int main()
if (file == NULL)
return 1;
C Programming Language File Handling
}
while(1)
int ch = fgetc(file);
if (ch == EOF)
break;
printf("%c",ch);
fclose(file);
return 0;
int main()
if (file == NULL)
return 1;
while(1)
char ch = getchar();
if (ch == EOF)
break;
putc(ch,file);
}
C Programming Language File Handling
fclose(file);
return 0;
struct Student
int RollNo;
char Name[30];
double Marks;
};
int main()
if (file == NULL)
return 1;
while(1)
scanf("%d",&s1.RollNo);
scanf("%s",&s1.Name);
scanf("%lf",&s1.Marks);
fprintf(file,"%d\t%s\t%lf\n",s1.RollNo,s1.Name,s1.Marks);
printf("Continue (Y/N)");
char ch = getch();
break;
fclose(file);
return 0;
int main()
if (file == NULL)
return 1;
while(1)
int cnt=fscanf(file,"%d\t%s\t%lf\n",&s1.RollNo,&s1.Name,&s1.Marks);
if (cnt == -1)
break;
printf("\n%d\t%s\t%lf\n",s1.RollNo,s1.Name,s1.Marks);
C Programming Language File Handling
fclose(file);
return 0;
Following are the Methods used for Reading and Writing in Binary Format:
size_t fwrite(const void *data, size_t size_of_element, size_t number_of_elements, FILE *a_file);
long ftell(FILE * a_file) returns the current file position in the file returns a long value
long position = ftell(fp);
struct Student
{
int RollNo;
char Name[30];
double Marks;
};
C Programming Language File Handling
int main()
{
struct Student s1,s2;
FILE *file = fopen("d:\\student.dat","w+b");
if (file == NULL)
{
printf("Error in saving student data");
return 1;
}
while(1)
{
printf("Enter RollNo: ");
scanf("%d",&s1.RollNo);
printf("Enter Name: ");
scanf("%s",&s1.Name);
printf("Enter Marks: ");
scanf("%lf",&s1.Marks);
//fprintf(file,"%d\t%s\t%lf\n",s1.RollNo,s1.Name,s1.Marks);
fwrite(&s1,sizeof(s1),1,file);
printf("Continue (Y/N)");
fflush(stdin);
char ch = getchar();
if (ch == 'N' || ch == 'n')
break;
}
fseek(file,0,0);
while(1)
{
int n = fread(&s2,sizeof(s2),1,file);
if (n == 0)
break;
printf("%d %s %lf\n",s2.RollNo,s2.Name,s2.Marks);
}
fclose(file);
C Programming Language File Handling
return 0;
}
return 0;
}