Você está na página 1de 14

File Handling in C

File Management
Functions such as printf and scanf to read and write data. This
works fine as long as data is small.
But it has two major problems:
It becomes cumbersome and time consuming to handle large
volume of data through terminals.
The entire data is lost when either the program is terminated or
the computer is turned off.
So it is necessary to have more flexible approach where data can
be stored on the disks and read whenever necessary, without
destroying data.


Files

Files: A file is a place on the disk where a group of related data
is stored. A file is collection of bytes stored on a secondary
storage device.
Computers store files to secondary storage so that the contents
of files remain intact when a computer shuts down.
When a computer reads a file, it copies the file from the storage
device to memory; when it writes to a file, it transfers data from
memory to the storage device.
FILE is a structure declared in stdio.h. We have to use file
pointer, a pointer variable that points to a structure FILE.

File Streams
A stream is an act representation of any external source or
destination for data, so the command line on your display and
files on disk are all examples of stream.
A stream is a series of bytes data flow from your program to a
file or vice-versa.
In C, we input/output data using streams. We can associate a
stream with a device (i.e. the terminal) or with a file.




Format of Streams
There are two formats of streams which are as follows
Text Stream
It consists of sequence of characters, depending on the
compilers
Each character line in a text stream may be terminated by a
newline character.
Text streams are used for textual data, which has a
consistent appearance from one environment to another or
from one machine to another.
Binary Stream
It is a series of bytes.
Binary streams are primarily used for non-textual data, which
is required to keep exact contents of the file.


FILE Streams
Stdin-Standard input
Stdout-Standard Output
Stderr-Standard Error
Stdaux-Standard Auxiliary
Stdprn-Standard printer
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
x=flushall();
printf("\n Number of opened stream are %d",x);
getch();
}
File handling in C
In C we use FILE * to represent a pointer to a file.
fopen is used to open a file.
It returns the special value NULL to indicate that it
couldn't open the file.
fopen(filename,mode);


File Operations
There are different operations that can be carried out on a file.
These are:
Creation of a new file
Opening an existing file
Reading from a file
Writing to a file
Moving to a specific location in a file (seeking)
Closing a file
Modes for opening files
The second argument of fopen is the mode in which we open
the file.
There are three modes:
"r" opens a file for reading
"w" opens a file for writing - and writes over all previous
contents
"a" opens a file for appending - writing on the end of the file
Opening & Closing Files
//Read data from keyboard and display it
//file name is input.txt ,//fopen,fclose,getc(),putc(),r,w,EOF
#include<stdio.h>
#include<conio.h>
void main()
{ FILE *f1;
char c;
clrscr();
printf("\n Data input in a file input.txt \n");
f1=fopen("input.txt","w");
while((c=getchar())!='\n')
\putc(c,f1);
fclose(f1);
printf("\n data output\n");
f1=fopen("input.txt","r");
while((c=getc(f1))!=EOF)
printf("%c",c);
fclose(f1);
getch(); }
Reading & Writing to Files
//fscanf(),fprintf(),stdin,stdout
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
I nt uid,i;
float per;
char name[25],branch[30],filename[30];
clrscr();
printf("input file name\n");
scanf("%s",filename);
fp=fopen(filename,"w");
printf("\n Input student data \n");
printf("UID \t Name \t Percentage \t Branch \n");
for(i=1;i<=10;i++)
{
fscanf(stdin,"%d%s%f%s",&uid,name,&per,branch);
fprintf(fp,"%d%s%.2f%s\n",uid,name,per,branch);
}
fclose(fp);
fprintf(stdout,"\n\n");
fp=fopen(filename,"r");
printf("UID \t Name \t Percentage \t Branch \n");

while(fscanf(fp,"%d%s%f%s",&uid,name,&per,branch)!=EOF)
printf("\n%d%s%f%s",uid,name,per,branch);
fclose(fp);
getch(); }

Error Situations in File Handling
Trying to use a file that has not been opened.
Opening a file with invalid name or extension.
Attempting to access awrite protected file.
Acces to file that does not exist.
Device Overflow.
Media error.
Trying to read beyond the end of file mark.

Thanks..

Você também pode gostar