Chapter 11

 

Files (2nd Note)

 

File is a data structure where we can permanently store the data. FILE is a data structure which helps us to store our data permanently on the secondary storage devices (e.g. hard drive, floppy disks etc).

One can create, open, modify and close a file. The syntax for creating file is as follows:

FILE *fp;

Where FILE must be in capital letter and fp is a pointer of the FILE type.

 

 

Processing a file

 

The processing of a file involves following steps:

  1. Opening a file
  2. Reading from or writing onto a file
  3. Closing the opened file

 

 

Opening a file:

 

Opening a file establishes an understanding between the program and the operating system. This provides operating system with a name of the file and the mode in which the file is to be opened i.e. whether for reading or writing.

 

The syntax for opening a file in standard I/O package is

 

            fptr=fopen(“fileopen”,”mode”);

e.g. fptr=fopen(“test.dat”,”w”);

 

Mode                                       Purpose

r                                              Open for reading. The file must already exist.

w                                             Open for writing. If the file already exists, its contents will be overwritten . If doesn’t exist, it will be created.

a                                              Open for append. Data will be added to the end of the existing file, if file already exists

r+                                            Open for both reading and writing. The file must already exist.

w+                                           Open for both reading and writing. If the file exists, its contents are over written.

a+                                            Open for both reading and appending

 

 

The request for opening a file may be granted or rejected. In case of rejection, the program will not run and will terminate abnormally. There may be many reasons for the rejection of program request, for e.g.

  • Trying to open a file for reading which doesn’t exist.
  • Trying to open a file for writing, but sufficient space is not available on the disk.
  • Trying to open a file that has been corrupted

If the file cannot be opened, the fopen () functions returns a NULL value.

 

//Program to demonstrate writing of one character at a time to a file

 

FILE *fptr;

char ch;

 

if ((fptr=fopen (“file1.dat”,”w”))==NULL)

{

printf (“Unable to open file”);

exit(1);

}

printf(“\n Type a line of text, then hit enter”);

while((ch=getch())!=’\n’)

fputc(ch, fptr);

fclose(fptr);

}

 

 

Reading from a file

The syntax of the function that reads and returns one character at a time

ch=fgetc(fptr);

//program to demonstrate reading

 

FILE *fptr;

char ch;

if((fptr=fopen(“file.dat”,”r”))==NULL)

{

printf(“Unable to open file”);

exit(1);

}

while((ch=fgetc(fptr))!=EOF)

putchar(ch);

fclose(fptr);

}

 

 

Therefore, a program can check the value returned by the fopen() function to determine whether the file is opened successfully or not.

            fptr=fopen(“test.dat”,”w”);

      if(fptr==NULL)

{

printf(“\nCannot open file test.dat\n”);

exit(1);

}

If the function returns NULL, then the message is printed and the exit() function is executed, causing the program terminate immediately and avoid the situation of trying to read from or write onto a non-existent file.

 

 

 

Closing a file

When a program has been finished with reading/writing of a file, it must be closed. This task is carried out by using fclose() library function, whose syntax is

fclose(fptr);

 

 

 

 

 

 

#Character Input/Output-

Using character I/O, data can be read or write one character at a time.

Once the program has established a line of communication with a particular file by opening it, then it can write to the file. The syntax of function that writes one character at a time is

            fputc(ch,fptr);

 

 

 

#String input/Output:-

 

Reading and writing strings of character is an easy. This is analogous to puts ( ) and gets ( ) functions that write data to screen and read data from the keyboard.

The syntax of the function that writes a string of characters at a time is

fputs (str,fptr);

The syntax of the function that reads a string from a file

            fgets (str,n,fptr);

 

//program to demonstrate writing of strings

 

FILE *fptr;

char str[80];

if(fptr=fopen(“file.txt”,”w”))==NULL)

{

printf((“Unable to open”);

exit(0);

}

printf(“Enter a set of string”);

while(strlen(gets(str))>0)

{

fputs(str,fptr);

fputs(“\n”,fptr);

}

fclose(fptr);

}

 

//program to demonstrate reading of strings

 

FILE *fptr;

char str[80];

if((fptr=fopen(“file.txt”,”r”))==NULL)

{

printf(“Unable to open”);

exit(1);

}

while(fgets(str,80,fptr)!=NULL)

{

puts(str);

}

fclose(fptr);

}

 

 

 

The important file handling functions that are available in C library has been listed in the table below:

 

Function                                 Operation

fopen()                 open an existing file for use

fclose()                close a opened file

getc()                  Read character from a file

putc()                  writes a character to a file

fprintf()               writes a set of data values to a file

fscanf()                reads a set of data values from a file

getw()                  reads an integer feom a file

putw()                  writes an integer to a file

fseek()                 sets a position to a desired point in the file

ftell()                 gives the current position in the file

rewind()                sets the position to the beginning of the file

 Full Width

Post a Comment

0 Comments