Search This Blog

Saturday, April 25, 2020

File handling in C Programming

         C-Programming:-

In this tutorial, you will learn:-

What is the file?



* Computer stores files to secondary storage so that the content of files remain intact when a computer tur off.
* When a computer reads a file, it copies the file from the storage devices to memory; when it writes to a file, it transfers data from memory to the storage device.
* C uses a structure called FILE ( defined in stdio.h ) to store the attributes of a file.

Steps in processing a file :-

1. Create the file through pointer variable like:
FILE * p;
2. Open the file
3. Read or write data.
4. Close the file.

 The basic file operation are:-

* fopen : By this function we open a file.
* fclose  -  close the opened file.
* fread - to read entire records from a file.
* fwrite - to write an entire record into a file.
* ftell/ tgetpos - tell you where the file pointer is located.
* fputc - Write character in file.
* fgetc - Read character from a file.
* getw - Read number from a file.
* putw -  write number into a file.
* fputs - To write a string in a file.

 Mode
   Meaning
   r
 Open text file in read mode.
* If file exists, the maker is poistioned at begining.
* If file doesn't exist, it is created.
   w
 Open text file in write mode.
* If file exists, it is erased.
* If file doesn't exist, it is created.
    a
 Open tezt file in append mode.
* If file exists, the maker is positioned at end.
* If file doesn't exists, it is created.

Additionally:-

* "r+" (read + write ) in this mode we can also write and modify existing data. The file to be opened must exist and the previous data of the file is not erased. The mode is also called update mode.
* "w+"(write + read) if the file doesn't exist then a new file is created and if the file exists then the previous data is erased.

A structure named FILE is defined in the file stdio.h that contains all the information about the file like:-
* Name of file.
* Status.
* Buffer size.
* Current position.
* End of file status. 

 File open:-

The file function (fopen) server opens file

Syntax:-

FILE *fopen("filename ", "mode") ;

More on fopen :-

* On success fopen () return a pointer of type FILE and on error, it returns NULL.
* We asssign the return value of fopen to our pointer variable.
FILE * p1 ;
p1 = fopen("MYFILE.TXT", "W");
p1 = fopen("A:\\DOCUMENTS\\MYFILE.TXT","W");

Errors in fopen:-

* If an error occurs in opening a file, then fopen() return NULL.
FILE *p;
p=fopen("abc.txt","r");
if(p==NULL)
{
printf("error in opening file");
exist(1);
}

Errors may occur due to following reason:-

* If we try to open the file in read mode and if the file doesn't exist or we do not have read permission on that file.
* If we try to create a file but there is no space on disk or we don't have write permissions.
* If we try to create a file that already exists and we don't have permission to delete that file.
* Opening system limits the number of files that can be opened at a time and we are trying to open more files than that number.

Closing a file:-

*When we finish with mode, we need to close the file before ending the program or beginning another mode with the same file.
* To close a file, we use fclose
 There are some I\O function in file handling:-
* fprintf()

syntax:- 

 fprintf(fp,"string",variable);

For example:-

int i=10;
float x= 2.3;
char c='s' ;
FILE *f;
fp= fopen("out.txt","w");
fprintf("fp,'T%d%f%c",i,x,c);

 * fscanf()

Syntax:-    

 fscanf(fp,"string",identifiers) ;
 for example:-
FILE *fp ;
Fp= fopen("input.txt","r");
int i;
fscanf(fp,"%d",i);

*getc() :-

syntax:-   

identifier = getc(file pointer) ;
for example:-
FILE *fp ;
fp =fopen("input.txt","r");
char ch ;
ch getc(fp);

* putc():-

Write a character.

for example:-

FILE *fp;
char ch;
putc (ch,fp) ;

End of file:-

There are a number of ways to test for the end of life condition.
FILE *fptr1;
int istatus;
istatus = fscanf(fptr,"%d",&var);
if( istatus == feof(fptr1) 
{
printf("end of file encounted \ n ");
}

Reading and writing files:-

#include<stdio.h>
int main()
{
FILE *outline , *intile ;
int b = 5 , f;
float a = 13.3 , c =16.68 , e , g ;
outline =  fopen ("testdata","r");
fprintf(outline,"%f%d%f",a,b,c);
fclose (outline) ;
 infile= fopen("testdata","r");
fscanf(infile,"%f%d%f ", &e, &f , &g);
printf("%f%d%f \n",a,b,c);
printf("%f%d%f \ n",e,f,g) ;
}
return(0);
}

Example:-

#include<stdio.h>
int main()
{
cgar ch ;
FILE *fp ;
fp =  fopen ("out.txt","r");
while(!feof(fp) )
{
ch = getc(fp) ;
printf("\n%c",ch);
}
return(0) ;
}

fread:-

Declaration :-

a fread(void *ptr, a ,a , FILE *stream) ;

Remark :-

fread reads a specific number of equal sized data items from an input stream into a block.
Size      = Length of each items read, in bytes
n           =  Number of items  read .
stream = File pointer

Example:- 

#include<stdio.h>
int main()
FILE *f ;
char buffer [11] ;
If(f= fopen("fred.txt","r"));
{
fread (buffer, 1 , 10 , f) ;
 buffer [10] = 0 ;
fclose (f);
printf("first 10 character of the file :\ n%s\n",buffer);
}
return 0;
}

fwrite()

Declaration :-

a fwrite(void *ptr,  size, a, FILE *stream);

For example:-

#include<stdio.h>
#include<conio.h>
void main()
{
char a[10] = {'1','2','3','4','5','6','7','8,'9','a'};
FILE *fs;
fs=fopen("project.txt","w");
fwrite(a,1,10,fs);
getch();
}

fseek():-

This function sets the file position indicator for the stream pointed to by stream or you can say it seek a specified place within in a file and modify it.
SEEK_SET       Seeks from beginning of file.
SEEK_CUR      Seeks from current position.
SEEK_END      Seeks from end of file.

Example:-

#include<stdio.h>
int main()
{
FILE *f;
f = fopen("mylife.txt,"w");
fputs("hello world",f);
fseek(f,6, SEEK_SET) ;          SEEK_CUR , SEEK_END
fputs("india",f) ;
return 0 ;
}  

ftell ()

Offset = ftell(file pointer) ;
"ftell" returns position for input or output on the file.
#include<stdio.h>
int void 
{
FILE *stream ;
stream = fopen("mylife.txt",'w");
fprintf(stream, "This is a test");
printf("%ld \ n", ftell(stream));
fclose (stream);
return 0 ;
}

Mr. abhijeet dwivedi

Developer

Hey, I'm Abhijeet

0 comments:

Post a Comment

If you have any doubt, please let me know