File Handling In C Computer Programming Language.



  To familiarize students with how to open a file, reading from a file and writing into a file.    


Turbo C IDE 
file handling


Writing C language program to perform the following tasks
       Open a text file
       Read from text file
       Write into text file
File – a file represent a sequence of byte on the disk where a group of related data is stored. File is created for permanent storage of data. It is ready made structure. In C, we use a structure pointer of the file type to declare a file. C programming language provides number of functions to perform basic file operations. Few of them are as under: 
Create a new file or open an existing file
Closes a file
Reads a character from a file
Writes a character to a file
Reads a set of data from a file
Writes a set of date to the file
Reads an integer from a file
Opening Files – fopen() function is used to create a new file or open an existing file, this call will initialize an object of the type FILE, which contains all the information necessary to control the stream.
Following is the prototype of this function call

FILE *fopen ( const char *filename, const char *mode );

Here, filename is string literal, which you will use to name your file and access mode can be many types such as r, w, a, as open an existing text file for reading purpose, opens a text file for writing, opens a text file for writing in appending mode respectively.
Writing to a file - The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error. Following is simplest function to write individual character to a stream.
int fputc ( int c, FILE *fp)

Reading from a file - The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error it returns EOF. Following is simplest function to read a single character from a file.
int fgetc (FILE *fp) 

Post a Comment