File Handling
File Handling
File Handling
by
Dr. Subodh Srivastava
CSE,IIT (BHU)
WHAT IS A FILE ?
Wherever there is a need to handle large
volumes of data, it is advantageous to store data
on the disks and read whenever necessary.
This methods employs the concept of files to
store data.
A file is a place on disk where a group of related
data is stored.
a
Open the file for appending (or adding) data to it.
FILE *fptr;
fptr=fopen(Text.DAT, w);
if(!fptr)
else
..
..
fclose(fptr);
}
Once a file is opened it has own FILE structure having information about
the file being used, such as its current size, its location in computer
memory, etc. It has a character pointer that points to the character to be read
or written.
Function Name
Operation
fopen()
fclose()
getc()
putc ()
fprintf ()
fscanf()
getw()
putw()
FILE *fptr;
if(!fptr)
exist();
....
....
while((ch=getchar()) !=EOF)
{
putc(ch,fptr);
fclose(fptr);
fptr=fopen(TEXT.DAT, r); /*open file to input data*/
if(!fptr)
printf(\nCant open file for reading\n);
else
{
printf(\nThe text stored in the file is :\n\n);
while( (ch=getc(fptr)) ! EOF)
{
printf(c,ch);
}
fclose(fptr);
}
}
The fil e name is again represented for reading. The file contents are read
character by character and displayed on the screen using statement.
ch=getc(fptr)
When getc () encounters the end-of-file mark i.e, EOF, the reading from the
ends. If end of file condition is not tested then either an infinite loop may start
or program may terminate abonormally.
Note: getc() and putc() are macros,whereas fgetc() and fputs are their
function versions.
/* read a text file and create another file in which multiple blanks are replaced
by a single one */
#include<stdio.h>
main()
{
FILE *fptr1, *fptr2;
char source[13],target [13];
char ch;
int flag=1;
clrscr();
printf(Enter source file name to be copied\n\n);
gets(source);
if (! fptr1)
else
while(flag)
ch=fgetc(fptr1);
if(ch==EOF)
breaks;
fputc(ch,fptr2);
if (ch == )
while (ch = = )
ch=fgetc(fptr1);
if( ch = = EOF)
flag=0;
fputc (ch,fptr2);
Closing a file
We can close a file simply using fclose and the
file pointer. Here's a complete "hello files".
FILE *fptr;
char filename[]= "myfile.dat";
fptr= fopen (filename,"w");
if (fptr == NULL) {
printf ("Cannot open file to write!\n");
exit(-1);
}
fprintf (fptr,"Hello World of filing!\n");
fclose (fptr);
Reading loops
It is quite common to want to read every
line in a program. The best way to do this
is a while loop using fgets.
/* define MAXLEN at start using enum */
FILE *fptr;
char tline[MAXLEN]; /* A line of text */
fptr= fopen ("sillyfile.txt","r");
/* check it's open */
while (fgets (tline, MAXLEN, fptr) != NULL) {
printf ("%s",tline); // Print it
}
fclose (fptr);
fscanf.c
#include <stdio.h>
#include <stdlib.h>
int main ( )
{
double x ;
FILE *ifp ;
/* for exit */
Detecting end-of-file
with fscanf
When reading an unknown number of data
elements from a file using fscanf( ), we
need a way to determine when the file has no
more data to read, i.e, we have reached the end
of file.
Fortunately, the return value from fscanf( )
holds the key. fscanf( ) returns an integer
which is the number of data elements read from
the file. If end-of-file is detected the integer
return value is the special value EOF
Structures in C
In C, we can create our own data types - FILE is
an example of this.
If programmers do a good job of this, the end user
doesn't even have to know what is in the data type.
struct is used to describe a new data type.
typedef is used to associate a name with it.
int, double and char are types of variables.
With struct you can create your own. It is a
new way to extend the C programming language.
Typedef
Typedef allows us to associate a name with
a structure (or other data type).
Put typedef at the start of your program.
typedef struct line {
int x1, y1;
int x2, y2;
} LINE;
int main()
{
LINE line1;
}
Complex
no
functions
typedef struct complex {
float imag;
float real;
} CPLX;
CPLX mult_complex (CPLX, CPLX);
int main()
{
/* Main goes here */
}
CPLX mult_complex (CPLX no1, CPLX no2)
{
CPLX answer;
answer.real= no1.real*no2.real - no1.imag*no2.imag;
answer.imag= no1.imag*no2.real + no1.real*no2.imag;
return answer;
}
This is a CAST
sizeof(char) returns how
we want
(remember them)
much memory a char
n chars
that forces the variable
takes
to the right type (not
needed)
This says in effect "grab me enough memory for 'n' chars"
Free
The free statement says to the computer "you
may have the memory back again"
free(sieve);