File Operation in C Language
A file represents a sequence of bytes on the disk where a group of related data is stored. File is created
for permanent storage of data. It is a ready-made structure.
In C language, we use a structure pointer of file type to declare a file.
FILE *fp;
C provides a number of functions that helps to perform basic file operations. Following are the functions,
Function description
fopen() create a new file or open a existing file
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from a file
fprintf() writes a set of data to a file
getw() reads a integer from a file
putw() writes a integer to a file
fseek() set the position to desire point
ftell() gives current position in the file
rewind() set the position to the begining point
Opening Files
You can use the fopen( ) function to create a new file or to 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:
Here, filename is string literal, which you will use to name your file and access mode can have one of the
following values:
If you are going to handle binary files then you will use below mentioned access modes instead of the above
mentioned:
Closing a File
To close a file, use the fclose( ) function. The prototype of this function is:
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. The EOF is a constant defined in the header file stdio.h.
There are various functions provide by C standard library to read and write a file character by character or in
the form of a fixed length string. Let us see few of the in the next section.
Writing a File
Following is the simplest function to write individual characters to a stream:
int fputc( int c, FILE *fp );
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. You can use the following
functions to write a null-terminated string to a stream:
int fputs( const char *s, FILE *fp );
The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value
on success, otherwise EOF is returned in case of any error. You can use int fprintf(FILE *fp,const char *format,
...) function as well to write a string into a file. Try the following example:
#include <stdio.h>
main() {
FILE *fp;
fp = fopen("test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
When the above code is compiled and executed, it creates a new file test.txt in directory and writes two lines
using two different functions. Let us read this file in next section.
Reading a File
Following is the simplest function to read a single character from a file:
int fgetc( FILE * fp );
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. The following functions allow you to read a string from a stream:
char *fgets( char *buf, int n, FILE *fp );
The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read
string into the buffer buf, appending a null character to terminate the string.
If this function encounters a newline character '\n' or the end of the file EOF before they have read the
maximum number of characters, then it returns only the characters read up to that point including new line
character. 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.
#include <stdio.h>
main() {
FILE *fp;
char buff[255];
fp = fopen("test.txt", "r");
fscanf(fp, "%s", buff);
printf("1: %s\n", buff );
fgets(buff, 255, fp);
printf("2: %s\n", buff );
fgets(buff, 255, fp);
printf("3: %s\n", buff );
fclose(fp);
When the above code is compiled and executed, it reads the file created in previous section and produces the
following result:
1 : This
2: is testing for fprintf...
3: This is testing for fputs...
A very special example which will help in programming contest:
Here both read and write operation showing together.
#include <stdio.h>
int main ()
{
int a,b, sum;
freopen("in.txt","r",stdin);
freopen("out.txt", "w",stdout);
scanf("%d %d", &a, &b);
sum = a+b;
printf("%d",sum);
return 0;
}
===========****===========