Archive for 10. File I/O Functions in C

File I/O Functions in C

C File Handling-File Pointers.

Use a new datatype called a file pointer to with C files . This type is written as FILE *, and defined within stdio.h. A file pointer called output_file is declared in a statement is as follows:
FILE *output_file;

Function Name
Creates a new file for use
Opens a new existing file for use
Closes a file which has been opened for use
Reads a character from
Writes a character to a file
Writes a set of data values to a file
Reads a set of data values from a file
Reads a integer from a file
Writes an integer to the file
Sets the position to a desired point in the file
Gives the current position in the file
Sets the position to the begining of the file

Opening a file pointer using fopen()
To open streams,fopen() is used.For opening files for input, this is most often used. Before it can access it,your program must open a file . This function returns the required file pointer. The value NULL will be returned,if the file cannot be opened for any reason. fopen’s prototype is as follows:
FILE *fopen (const char *path, const char *mode);
fopen takes in the path to the file as well as the mode and to open the file with. Take for the following example:
iFILE *Fp; Fp = fopen(“/home/johndoe/input.dat”, “r”);
For reading,this will open the file in /home/johndoe/input.dat . You will usually use fopen as given below:
if ((output_file = fopen(“output_file”, “w”)) == NULL)
fprintf(stderr, “Cannot open %s\n”, “output_file”);

1. “r”-Opening the file for reading
2. “a”-Opening the file for appending
3. “w”-Opening the file for writing
4. “w+”-open for the writing and reading (existing file will be overwritten)
5. “r+”-open for the reading and updating (file must already exist)
6. “a+”-open for the appending and reading (file may or may not exist)
The following code fragmentis used to get the text from the first
FILE *output_file;

Closing a file pointer using fclose()
You would use fclose() to close the stream, . The prototype for fclose is given below:
int fclose( FILE *stream );

File I/O :: fgets()
The alternatives to scanf/fscanf is fgets. The prototype is as follows:
fgets stores it into *s pointer and reads in size – 1 characters from the stream. The string is always automatically null-terminated. If it reaches an EOF or newline,fgets stops reading in characters.

File I/O :: sscanf()
The sscanf library call is handy to scan a string for a format.The prototype is as follows:
sscanf takes a character pointer instead of a file pointer and works much like fscanf. Instead of scanf/fscanf,using the combination of fgets/sscanf you can avoid the “digestion” problem (or bug, depending on who you talk to:)

File I/O :: fprintf()
· It is sometimes useful also to output to the different streams.
· fprintf() allows us to do exactly the same thing
· The prototype for fprintf is as follows:
int fprintf(FILE *stream, const char *format, …);
fprintf takes in a special pointer called a file pointer, signified by the FILE *. It then accepts argument and a formatting string and. The only difference between printf and fprintf is that fprintf can redirect output to a particular stream. These streams can be stderr,stdout,or a file pointer. More on the file pointers when we get to fopen. An example is given below:
fprintf(stderr, “ERROR: Cannot malloc enough memory.\n”);
This output the error message to standard error.

File I/O :: fscanf()
fscanf() is basically a streams version of the scanf. The prototype for fscanf is as follows:
int fscanf( FILE *stream, const char *format, …);
File I/O :: fflush()
If the program crashes, sometimes the stream isn’t written. You can do this by using fflush() function. Sometime it is necessary to forcefully flush a buffer to its stream. The prototype for fflush is as follows:
int fflush(FILE *stream);
Not very difficult to use, specify the stream to fflush.
The program which is given below displays use of a file operations. The the program writes it and data enter through the keyboard. Character by character, to file input. The end of the data is indicated by entering an EOF character, which is control-z. the file input is closed at this signal only.
#include< stdio.h >

file *f1;
printf(“Data input output”);
f1=fopen(Input,w); /*Open the file Input*/
while((c=getchar())!=EOF) /*get a character from key board*/
putc(c,f1); /*write a character to input*/
fclose(f1); /*close the file input*/
printf(“\nData output\n”);
f1=fopen(INPUT,r); /*Reopen the file input*/

Leave a Comment


Get every new post delivered to your Inbox.

Join 26 other followers