Sunday, January 04, 2015

File Handling in C

Posted By: BackBenchers World - Sunday, January 04, 2015
A file is a place on disk where a group of related data is stored. Like most other High Level Languages, C supports a number of functions that have the ability to perform basic file operations which includes:

1. Naming the file
2. Opening a file
3. Closing the file
4. Reading data from a file
5. Writing data to a file

Functions of file handling

Function Name Operation                                                  
fopen() : Create a file for use and/or Open an existing file.
fclose() : Close a file which has been opened.
getc() : Read a character from a file.
putc() : Write a character to a file.
fprintf() : Write a set of data values to a file.
fscanf() : Read a set of data values from a file.
getw() : Read an integer from a file.
putw() : Read an integer into a file.
fseek() : Set the position to the desired point in a file.
ftell() : Give current position in the file.
rewind() : Set the position to the beginning of a file.

Defining and Opening a file
For defining a file we have to include three things
  • File name
  • Data Structure
  • Purpose
File name is a string of characters that make a valid file name for operating system. E.g., ABC.C, XYZ.txt etc.

Data Structure of a file is defined as FILE in the library of Standard Input Output function definition.

Purpose includes that in which mode we are to open a file i.e., read(r), write(r) or append(a) mode.

Syntax for defining a file
FILE *fp;
fp=fopen("File_name","mode");

Here FILE is a data structure and fp is a pointer of type FILE.

Simple Program to Read data from keyboard and write it to a file "File_name" and again read the same data from file the same file and display it on the screen.

#include<conio.h>
#include<stdio.h>

void main()
{
FILE *fp;
char c;

printf("Enter data into file:\n");
fp=fopen("File_name","w");

while((c=getchar())!=EOF)
{
putc(c,fp);
}
fclose(fp);

printf("Data Output:\n");

fp=fopen("File_name","r");

while((c=getc(fp)!=EOF)
{
printf("%c",c);
}
fclose(fp);
getch();
}

Error Handling in C
It is possible that an error may occur during Input/Output operation on a file. Typical error situation include

1. Trying to read beyond the EOF (End Of File).
2. Device Overflow.
3. Trying to use a file that has not been opened.
4. Trying to perform an operation on a file, when the file is opened for another type of operation.
5. Opening an file with an invalid name.
6. Attempting to write to a write protected file.

C language provides some error handling functions to handle these kind of errors
  • feof(): This function can be used to test for End of File condition. It takes a file pointer as its argument and returns a non-zero integer value if all the data from the specified file has been read. It returns zero otherwise.
  • ferror(): This function reports the status of the indicated. It takes file pointer as its argument and returns a non-zero value if an error has been detected up to that point during the processing. It returns zero otherwise.

Copyright © 2013 TechDotHunter™ is a registered trademark.

Designed by Templateism. Hosted on Blogger Platform.