File Handling Functions

 

Function Syntax Example
fopen - Open file FILE * fopen(char * filename, char * mode)

Filename – string filename with path

Mode – mode of opening

r    read only

w   write only

a    append file

            +    read and write (r+/w+/a+)

 

Return value – FILE pointer on success

NULL on failure

FILE * infile ;

Infile = fopen(“a.txt”,”r”);

 

 

FILE *outfile = fopen(“b.txt”,”w”);

if(infile==NULL || outfile==NULL)

{

printf(“error”);

}

fclose – close the open file fclose(FILE *fptr)

 

fptr – FILE * of the open file

Returns void

fclose(infile);
fgetc – read one char from file int fgetc(FILE *fptr)

 

fptr – FILE * of the open file

returns – character read

-1 on eof or error

while( (ch=fgetc())!=-1)

putchar(ch);

//display content of file

//on screen

fputc – write one character to the file int fputc(int ch,FILE *fptr)

ch – character to write

fptr – open file pointer

returns character written

EOF if there is error

for(ch=’A’;ch<=’Z’;ch++)

fputc(ch,fptr);

fscanf 

Formatted read from file

int fscanf(FILE *ptr,char * fmt,…)

 

ptr – FILE pointer

fmt – format specifier for variables to be read

Returns – number of values read successfully

fscanf(infile, “%d %d”,&n1,&n2);
fprintf – formatted write to file   int fprintf(FILE *ptr,char * fmt,…)

 

ptr – FILE pointer

fmt – format specifier for variables to be read

Returns – number of characters  printed or negative number on error

fprintf(outfile,“Value is %d\n”,n1);
fread – read from binary file int fread( void *buffer, size_t size, size_t num, FILE *ptr )

 

buffer – stores the value read

size – size of the buffer

num – number of blocks to be read

ptr – file pointer

Returns – number of values read

 

Char *str = malloc(50);

fread(str,50,1,infile);

fwrite – write to a binary file int fwrite( void *buffer, size_t size, size_t num, FILE *ptr )

 

buffer – stores the value read

size – size of the buffer

num – number of blocks to be read

ptr – file pointer

Returns – number of values written

 

for(i=0;i<10;i++)

fwrite(&i,sizeof(i),1,outfile);

char a[]=”end”;

fwrite(a,strlen(a)+1,1,outfile);

fseek – move the file pointer by given offset int fseek(FILE*ptr,long offset,int whence)

 

ptr – file pointer

offset – offset in bytes from third parameter

whence – SEEK_SET – from beginning of file

SEEK_CUR – from current position

SEEK_END – from end of file

Returns – zero on success

Non-zero on failure

if(fseek(infile,4L,SEEK_SET)==0)

{

char ch=fgetc(infile);

printf(“The fourth char of the file is %c\n”,ch);

}

ftell – get the position of file pointer int ftell(FILE *ptr)

 

ptr – FILE pointer

Returns – position of file pointer

-1 on error

FILE *ptr = fopen(b[1],”r”);

fseek(ptr,0L,SEEK_END);

int size_of_file = ftell(ptr);

rewind – moves the file pointer to the beginning of the file void rewind(FILE *ptr)

 

ptr – FILE pointer

rewind(infile);

int n = fscanf(infile,”%d”,&n);

 

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback