Files in C

Loading....

Loading....

Loading....

Loading....

Loading....

Full text

(1)

1

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA

File Handling in C

Definition of a File

File is a set of records that can be accessed through the set of library functions.

Stream

Stream means reading and writing of data. The streams are designed to allow the user to access the files efficiently. A stream is a file or physical device like keyboard, printer and monitor. The FILE object uses these devices.

The FILE object contains all the information about stream like current position, pointer to any buffer, error and EOF (end of file).

File Types

There are two types of files.

Sequential Access File

Random Access File

Sequential Access File

In this file the records are kept sequentially. If we want to read the last record of the file we need to read all the records before that record. It takes more time. For example if we desired to access the 10th record then the first 9 records should be read sequentially for reaching to the 10th record.

Random Access File

In this type data can be read and modified randomly. In this type if we want to read the last record of the file, we can read it directly. It takes less time as compared to sequential file.

Steps for File operation

There are three steps for the file operation in C.

Opening a File.

Reading or Writing file.

Closing file.

(2)

2

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA File Handling Function

The C programming language supports many more file handling functions that are available in standard library. These functions are listed in the below table.

Function Operation

fopen() Creates a new file for read/write operation

fclose() Closes a file associated with file pointer

closeall() Closes all opened files with file pointer

fgetc() Reads the character from current pointer position and advances the pointer to next character.

getc() Same as fgetc()

fputc() Writes character one by one to a file

putc() Same as fputc()

gets() Reads string from the file

puts() Writes string to the file

fprintf() Writes all types of data values to the file.

fscanf() Reads all types of data values from a file

fseek() Sets the pointer position anywhere in the file

feof() Detects the end of the file

ferror() Reports error occurred while read/ write operation

perror() Prints compiler error messages along with user defined messages.

rename() Changes the name of the file.

Opening a File

A file has to b opened before beginning of read and write operations. Opening of a file creates a link between the operating system and the file functions.

When we open a file we must also specify what we wish to do with it i.e. Read from the file,

Write to the file, or both.

(3)

3

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA

Every file we open has its own file pointer variable. When we wish to write to a file we specify the file by using its file pointer variable.

We can declare these file pointer variables as follows:

FILE *fp1, *fp2, *fp3;

The variables fp1, fp2, fp3 are file pointers.

The file <stdio.h> contains declarations for the Standard I/O library and should always be

included at the very beginning of C programs using files.

The only one function to open a file is fopen().

Syntax for fopen()

fp1=fopen(“D:/abc.txt”,”r”);

The above statement opens a file called abc.txt for reading and associates the file pointer fp with the file.

Closing a file:

The file that is opened from the fopen() should be closed after the work is over i.e., we need to close the file after reading and writing operations are over.

Example:

fclose(fp);

this statement closes the file associated with the file pointer fp.

Text Modes

1. w (write)

This mode opens a new file on disk for writtng. If the file already exists, it will be overridden without confirmation.

Syntax:

fp=fopen(“D:/abc.txt”,”w”);

Here, abc.txt is the file name and “w” is the mode.

2. r (read)

This mode opens a pre-existing file for reading. If the file doesn’t exist, then compiler returns NULL to the file pointer.

Syntax:

(4)

4

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA

printf(“File does not exist”);

3. a (append)

This mode opens a pre-existing file for appending data. If the file does not exist then new file is opened i.e., if the file does not exist then the mode of “a” is same as “w”.

Syntax:

fp=fopen(“D:/abc.txt”,”a”);

4. w+ ( Write + Read)

It searches for the file, if found its contents are destroyed. If the file does not found a new file is created. Returns NULL if fails to open the file. In this mode file can be written and read.

Syntax:

fp=fopen(“D:/abc.txt”,”w+”);

The following program illustrates the concept of opening a file in read mode*/

/* file.c: Display contents of a file on screen */

#include <stdio.h>

void main()

{

FILE *fp;

int c ;

clrscr();

fp=fopen("file.c","r");

if(fp==NULL)

{

printf("File not found");

}

else

{

printf("\n The content of the file is...\n");

c = getc(fp);

while (c!=EOF)

{

putchar(c);

c=getc(fp);

(5)

5

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA

}

fclose(fp);

getch();

}

Output:

The following is another program that illustrates the concept of opening a file in read and write mode.

#include<stdio.h>

#include<conio.h>

#include<process.h>

void main()

{

FILE *fp;

char ch;

clrscr();

//Opening a file in write mode

(6)

6

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA

//Entering the data into the file

printf("\nEnter data and press '.' to stop\n");

while((ch=getche())!='.')

{

fputc(ch,fp);

}

fclose(fp);

//Opening a file in read mode

fp=fopen("abc.txt","r");

if(fp==NULL)

{

printf("\n File not found");

exit(0);

}

else

{

printf("\n The content of the file is...\n\n");

while(!feof(fp))

{

ch=fgetc(fp);

printf("%c",ch);

}

fclose(fp);

}

getch();

}

(7)

7

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA Handling errors during I/O operations

While performing read or write operations sometimes we do not get the result successfully. The reason may be that the attempt of reading or writing the operation may not be correct.

The C-Language provides the following standard library functions to handle these type of errors during I/O operations.

ferror()

It is used to detect any error that might occur during read/write operation on a file. It returns ‘0’ when the attempt is successful otherwise non-zero in case of failure.

perror()

It is a standard library function which prints the error messages specified by the compiler.

The following program illustrates the above two functions…..

#include<stdio.h>

#include<conio.h>

#include<process.h>

void main()

{

FILE *fp;

char ch;

clrscr();

fp=fopen("abc.txt","w");

while(!feof(fp))

{

(8)

8

B.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCAB.Naresh, Assistant Professor in B.V.R.I.C.E, Dept., of MCA

if(ferror(fp))

{

perror("Permission Denied");

getch();

exit(1);

}

else

{

printf("%c",ch);

}

}

fclose(fp);

getch();

}

Output:

Important Questions

Explain about the files in C.

Explain about the Modes of text to open a file in C.

Explain about read and write mode to open a file in C.

Figure

Updating...

References

Updating...