Files in C
Files in C
Files are not only used for data. Our programs are also
stored in files.
The editor which you use to enter your program and save it,
simply manipulates files for you.
In order to use files we have to learn about File I/O i.e. how
to write information to a file and how to read information
from a file.
As you know, you can have many files on your disk. If you
wish to use a file in your programs, then you must specify
which file or files you wish to use.
1
Specifying the file you wish to use is referred to as opening
the file.
When you open a file you must also specify what you wish
to do with it i.e. Read from the file, Write to the file, or
both.
Every file you open has its own file pointer variable. When
you wish to write to a file you specify the file by using its
file pointer variable.
The variables fp1, fp2, fp3 are file pointers. You may
use any name you wish.
2
Constants such as FILE, EOF and NULL are defined in
<stdio.h>.
When we wish to access this file for I/O, we use the file
pointer variable fp to refer to it.
3
File I/O
The Standard I/O Library provides similar routines for file
I/O to those used for standard I/O.
c = getc(fp);
putc(c,fp);
4
/* file.c: Display contents of a file on
screen */
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c ;
fclose( fp );
}
We then read a character from the file. This file must exist
for this program to work.
5
The function fclose is used to close the file i.e. indicate
that we are finished processing this file.
6
/* cat2.c: Prompt user for filename and
display file on screen */
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c ;
char filename[40] ;
while ( c != EOF )
{
putchar(c);
c = getc ( fp );
}
fclose( fp );
}
7
The above programs suffer a major limitation. They do not
check whether the files to be used exist or not.
if ( fp == NULL)
{
printf(“Cannot open %s for reading \n”, filename
);
exit(1) ; /*Terminate program: Commit suicide !!
*/
}
8
Alternatively, you could prompt the user to enter the
filename again, and try to open it again:
fp = fopen (fname, “r”) ;
while ( fp == NULL)
{
printf(“Cannot open %s for reading \n”, fname );
printf(“\n\nEnter filename :” );
gets( fname );
9
/*count.c : Count characters in a file*/
#include <stdio.h>
void main()
/* Prompt user for file and count number of
characters
and lines in it*/
{
FILE *fopen(), *fp;
int c , nc, nlines;
char filename[40] ;
nlines = 0 ;
nc = 0;
if ( fp == NULL )
{
printf(“Cannot open %s for reading \n”, filename
);
exit(1); /* terminate program */
}
c = getc( fp ) ;
while ( c != EOF )
{
if ( c == ‘\n’ )
nlines++ ;
nc++ ;
c = getc ( fp );
}
fclose( fp );
if ( nc != 0 )
{
printf(“There are %d characters in %s \n”, nc,
filename );
printf(“There are %d lines \n”, nlines );
}
10
else
printf(“File: %s is empty \n”, filename );
}
display character
if linecount == 20 then
begin
linecount = 1 ;
Prompt user and get reply;
end
11
/* display.c: File display program */
/* Prompt user for file and display it 20 lines at a
time*/
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c , linecount;
char filename[40], reply[40];
linecount = 1 ;
reply[0] = ‘\0’ ;
c = getc( fp ) ; /* Read 1st character if any
*/
while ( c != EOF && reply[0] != ‘Q’ && reply[0] !=
‘q’)
{
putchar( c ) ; /* Display character */
if ( c == ‘\n’ )
linecount = linecount+ 1 ;
if ( linecount == 20 )
{
linecount = 1 ;
printf(“[Press Return to continue, Q to
quit]”);
gets( reply ) ;
}
12
c = getc ( fp );
}
fclose( fp );
}
The string reply will contain the users response. The first
character of this will be reply[0]. We check if this is ‘q’
or ‘Q’. The brackets [] in printf are used to distinguish
the programs message from the file contents.
if ca == cb then
printout(“Files identical”);
else
printout(“Files differ”);
14
/* compare.c : compare two files */
#include <stdio.h>
void main()
{
FILE *fp1, *fp2, *fopen();
int ca, cb;
char fname1[40], fname2[40] ;
16
Writing to Files
The previous programs have opened files for reading and
read characters from them.
Example:
Write a file copy program which copies the file
“prog.c” to “prog.old”
Outline solution:
Open files appropriately
Check open succeeded
Read characters from prog.c and
Write characters to prog.old until all characters
copied
Close files
17
The step: “Read characters .... and write ..” may be refined
to:
18
/* filecopy.c : Copy prog.c to prog.old */
#include <stdio.h>
void main()
{
FILE *fp1, *fp2, *fopen();
int c ;
20
The above program only copies the specific file prog.c to
the file prog.old. We can make it a general purpose
program by prompting the user for the files to be copied
and opening them appropriately.
/* copy.c : Copy any user file*/
#include <stdio.h>
void main()
{
FILE *fp1, *fp2, *fopen();
int c ;
char fname1[40], fname2[40] ;
22
23