File handling functions in C
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.
Note that when a program ends all open files are closed automatically |
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 blocks from binary file
|
int
fread( void *buffer, size_t size, size_t num, FILE *ptr )
buffer – stores the pointer to 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 blocks to a binary file
|
int fwrite( void *buffer, size_t size, size_t num, FILE *ptr )
buffer – address of value to write
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);
|
but fopen and fclose command are not in the directories of linux and it shows error den wat to do??
ReplyDeletePlease mention what error you are getting.
DeleteHi,
Deletefopen and fclose are not the commands in Linux they are the C library functions. U can use them in a C file.
See the man page of fopen and fclose (man fopen) u will get your answer.
All the functions given in this post are C library functions. So you will not see them in bin folder in Linux directories.
Deletefopen and fclose are not the linux commands but they are the C inbuilt library functions for the file handling.
ReplyDeleteYou can use these functions in a C file to handling files. See the man page of fopen and fclose (man fopen).
File Handling in C
ReplyDeleteIts helpful for me