Files in C
Files in C
Generally, a file is used to store user data in a computer. In other words, computer stores the
data using files. we can define a file as follows...
Definition:
File is a collection of data that stored on secondary memory like hard disk of a computer.
C programming language supports two types of files and they are as follows...
Text Files (or) ASCII Files
Binary Files
Text File (or) ASCII File - The file that contains ASCII codes of data like digits, alphabets
and symbols is called text file (or) ASCII file.
Binary File - The file that contains data in the form of bytes (0's and 1's) is called as binary
file. Generally, the binary files are compiled version of text files.
File Operations in C
The following are the operations performed on files in c programming language...
1. Creating (or) Opening a file
2. Reading data from a file
3. Writing data into a file
4. Closing a file
All the above operations are performed using file-handling functions available in C. We
discuss file-handling functions in the next topic.
Creating (or) Opening a file
To create a new file or open an existing file, we need to create a file pointer of FILE type.
Following is the sample code for creating file pointer.
File *f_ptr ;
We use the pre-defined method fopen() to create a new file or to open an existing file. There
are different modes in which a file can be opened.
Consider the following code.
File *f_ptr ;
f_ptr = fopen("abc.txt", "w") ;
The above example code creates a new file called abc.txt if it does not exists otherwise it is
opened in writing mode.
In C programming language, there different modes are available to open a file and they are
shown in the following table.
S. No. Mode Description
5 w+ Opens a text file in both reading and writing mode. It set the cursor position to
6 a+ Opens a text file in both reading and writing mode. The reading operation is
performed from beginning and writing operation is performed at the end of the
file.
rewind( *file_pointer ) –
This function is used reset the cursor position to the beginning of the file.
Example Program to illustrate rewind() in C.
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
int position;
clrscr();
fp = fopen ("file.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d\n", position);
rewind(fp);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
getch();
return 0;
}
fseek( *file_pointer, numberOfCharacters, fromPosition ) –
This function is used to set the cursor position to the specific position. Using this function we
can set the cursor position from three different position they are as follows.
from beginning of the file (indicated with 0)
from current cursor position (indicated with 1)
from ending of the file (indicated with 2)
Example Program to illustrate fseek() in C.
#include<stdio.h>
#include<conio.h>
int main(){
FILE *fp;
int position;
clrscr();
fp = fopen ("file.txt", "r");
position = ftell(fp);
printf("Cursor position = %d\n",position);
fseek(fp,5,0);
position = ftell(fp);
printf("Cursor position = %d\n", position);
fseek(fp, -5, 2);
position = ftell(fp);
printf("Cursor position = %d", position);
fclose(fp);
getch();
return 0;
}
Error Handling in C
C programming language does not support error handling that are occured at program
execution time. However, C provides a header file called error.h. The header file error.h
contains few methods and variables that are used to locate error occured during the program
execution. Generally, c programming function returns NULL or -1 in case of any error
occured, and there is a global variable called errno which stores the error code or error
number. The following table lists few errno values and thier meaning.
3 No such process.
5 IO Error
10 No child processes
11 Try again
12 Out of memory
13 Permission denied
C programming language provides the following two methods to represent errors occured
during program execution.
perror( )
strerror( )
perror( ) - The perror() function returns a string passed to it along with the textual
representation of current errno value.
strerror( ) - The strerror() function returns a pointer to the string representation of the current
errno value. This method is defined in the header file string.h
Consider the following example program...
Example Program to illustrate error handling in C.
#include<stdio.h>
#include<conio.h>
int main(){
FILE *f_ptr;
f_ptr = fopen("abc.txt", "r");
if(f_ptr == NULL)
{
printf("Value of errno: %d\n ", errno);
printf("The error message is : %s\n", strerror(errno));
perror("Message from perror");
}
else {
printf("File is opened in reading mode!");
fclose(f_ptr);
}
return 0;
}
Programs on files:
else
{
while ((ch = fgetc(fp2)) != EOF)
fputc(ch,fp3);
}
fclose(fp3);