File Handling
File Handling
#include <stdio.h>
main( )
FILE *fp;
char stuff[25];
int index;
getch();
2013 Page 1
Ankur Rastogi Unit-VI File Handling
Reading (r)
When an r is used, the file is opened for reading, a w is used to indicate a file to
be used for writing, and an indicates that you desire to append additional data to
the data already in an existing file. Most C compilers have other file attributes
available.
#include <stdio.h>
void main()
FILE *fp;
int c;
fp = fopen("ABC.TXT","r");
c = getc(fp) ;
putchar(c);
c = getc(fp);
fclose(fp);
getch();
2013 Page 2
Ankur Rastogi Unit-VI File Handling
Writing (w)
When a file is opened for writing, it will be created if it does not already exist
and it will be reset if it does, resulting in the deletion of any data already there.
Using the w indicates that the file is assumed to be a text file.
#include <stdio.h>
int main()
FILE *fp;
fp = fopen("file.txt","w");
fclose(fp); /*done!*/
return 0;
Appending (a)
When a file is opened for appending, it will be created if it does not already
exist and it will be initially empty. If it does exist, the data input point will be
positioned at the end of the present data so that any new data will be added to
any data that already exists in the file.
#include <stdio.h>
int main()
FILE *fp;
2013 Page 3
Ankur Rastogi Unit-VI File Handling
fp = fopen("file.txt","a");
fclose(fp);
return 0;
int main ()
FILE *fp;
int c;
int n = 0;
fp = fopen("file.txt","r");
if(fp == NULL)
2013 Page 4
Ankur Rastogi Unit-VI File Handling
return(-1);
while(!feof(fp))
c = fgetc(fp);
printf("%c", c);
fclose(fp);
getch();
fseek()
When doing reads and writes to a file, the OS keeps track of where you are in
the file using a counter generically known as the file pointer. You can reposition
the file pointer to a different point in the file using the fseek() call. Think of it as
a way to randomly access you file.
Declaration
Following is the declaration for fseek() function.
Parameters
stream -- This is the pointer to a FILE object that identifies the stream.
whence -- This is the position from where offset is added. It is specified by one
of the following constants:
2013 Page 5
Ankur Rastogi Unit-VI File Handling
int main ()
FILE *fp;
fp = fopen("file.txt","w+");
fclose(fp);
getch();
fputc()
#include<stdio.h>
#include<conio.h>
void main()
FILE *fp;
char ch;
fp=fopen("Ab.txt","w");
for(ch='A';ch<='z';ch++)
fputc(ch,fp);
2013 Page 6
Ankur Rastogi Unit-VI File Handling
putchar(ch);
fclose(fp);
getch();
fscanf()
#include<stdio.h>
void main()
FILE *fp;
char name[30],str[20];
fp=fopen("A.txt","r");
printf("%s %s",name,str);
fclose(fp);
getch();
Closing a file
To close a file you simply use the function fclose with the file pointer in the
parentheses. Actually, in this simple program, it is not necessary to close the file
because the system will close all open files before returning to DOS, but it is
good programming practice for you to close all files in spite of the fact that they
will be closed automatically, because that would act as a reminder to you of
what files are open at the end of each program.
2013 Page 7
Ankur Rastogi Unit-VI File Handling
You can open a file for writing, close it, and reopen it for reading, then close it,
and open it again for appending, etc. Each time you open it, you could use the
same file pointer, or you could use a different one. The file pointer is simply a
tool that you use to point to a file and you decide what file it will point to.
#include <stdio.h>
main( )
FILE *fp;
char c;
fp = fopen("ABC.TXT", "r");
if (fp == NULL){
getch();
else {
do {
fclose(fp);
getch();
2013 Page 8