You are on page 1of 4

File handling in c

 File is a set of records that can be accessed through the set of library functions.
 A file represents a sequence of bytes, does not matter if it is a text file or binary file.
 C programming language provides access on high level functions as well as low level call to
handle file on your storage device.

Types of File
There are two tyoes of files.

 Sequential File
 Random Access File

Sequential File

 In this tyoe datas 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.

Random Access File

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

File Operations:-
There are different operations that can be carried out on a file. These are:

 Creation of a new file


 Opening an existing file
 Reading from a file
 Writing to a file
 Moving to a specific location in a file (seeking)
 Closing a file

 
Function Description
fopen() create a new file or open a existing file
fclose() closes a file
getc() reads a character from a file
putc() writes a character to a file
fscanf() reads a set of data from a file
fprintf() writes a set of data to a file
getw() reads a integer from a file
putw() writes a integer to a file
fseek() set the position to desire point
ftell() gives current position in the file
rewind() set the position to the begining point
feof() Detects the end of file.
ftell() Returns the current pointer position.
rename() Changes the name of file.

Random accessing of files in C language can be done with the help of the following functions −

 ftell ( )
 rewind ( )
 fseek ( )

ftell ( )
It returns the current position of the file ptr.
The syntax is as follows −
int n = ftell (file pointer)
For example,
FILE *fp;
int n;
_____
_____
_____
n = ftell (fp);
Note − ftell ( ) is used for counting the number of characters which are entered into a file.

rewind ( )
It makes file ptr move to beginning of the file.
The syntax is as follows −
rewind (file pointer);
For example,
FILE *fp;
   -----
   -----
   rewind (fp);
   n = ftell (fp);
   printf ("%d”, n);
Output
The output is as follows −
0 (always).

fseek ( )
It is to make the file pntr point to a particular location in a file.
The syntax is as follows −
fseek(file pointer, offset, position);
Offset
 The no of positions to be moved while reading or writing.
 If can be either negative (or) positive.
o Positive - forward direction.
o Negative – backward direction.
Position
It can have three values, which are as follows −

 0 – Beginning of the file.


 1 – Current position.
 2 – End of the file.
Example
 fseek (fp,0,2) - fp moved 0 bytes forward from the end of the file.
 fseek (fp, 0, 0) – fp moved 0 bytes forward from beginning of the file
 fseek (fp, m, 0) – fp moved m bytes forward from the beginning of the file.
 fseek (fp, -m, 2) – fp moved m bytes backward from the end of the file.

fread()
fread() function is used to read is a binary file. A Binary file is similar to the text file, but it
contains only large numerical data. Function Reads Block of Data from Binary Mode File and
Assign it to the Region of Memory Specified.[i.e Reads Block of Data From Binary File and
Assign it to Some Memory ]
fwrite()
fwrite() function is used to write is a binary file. Function Writes Block of Data to Binary Mode File.[i.e
Writes Block to Binary File]
Command Line Arguments in C
The arguments passed from command line are called command line arguments. These arguments are
handled by main() function.

To support command line argument, you need to change the structure of main() function as given below.

int main(int argc, char *argv[] )  

Here, argc counts the number of arguments. It counts the file name as the first argument.

The argv[] contains the total number of arguments. The first argument is the file name always.

#include <stdio.h>
void main(int argc, char *argv[] ) {
printf("Program name is: %s\n", argv[0]);
int a,b,c;
a=atoi(argv[1]);
b=atoi(argv[2]);
c=a+b;
printf("%d",c);

You might also like