Académique Documents
Professionnel Documents
Culture Documents
Description
fgets () fgets () function reads string from a file, one line at a time.
fputchar () fputchar () function writes a character onto the output screen from keyboard input.
SEEK_SET SEEK_SET moves file pointer position to the beginning of the file.
rewind () rewind () function moves file pointer position to the beginning of the file.
File operation
putw function is used to write an integer into a file. In a C program, we can write integer value in a file as
below.
putw(i, fp);
where
i – integer value
fp – file pointer
getw function reads an integer value from a file pointed by fp. In a C program, we can read integer value
from a file as below.
getw(fp);
This file handling C program illustrates how to write into a file using putw() function and how to read the
same file using getw() function.
#include <stdio.h>
int main ()
FILE *fp;
fp = fopen ("test.c","w");
putw(i,fp);
putw(j,fp);
putw(k,fp);
fclose(fp);
fp = fopen ("test.c","r");
while(getw(fp)!=EOF)
num= getw(fp);
printf(“Data in test.c file is %d \n”, num);
fclose(fp);
return 0;
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
int main ()
FILE *fp;
fp = fopen ("test.c","w");
putw(i,fp);
putw(j,fp);
putw(k,fp);
fclose(fp);
fp = fopen ("test.c","r");
while(getw(fp)!=EOF)
num= getw(fp);
fclose(fp);
return 0;
File operation
fgetc functions is used to read a character from a file. It reads single character at a time. In a C program,
we use fgetc() function as below.
fgetc (fp);
where,
fp – file pointer
# include <stdio.h>
int main( )
FILE *fp ;
char c ;
if ( fp == NULL )
return 1;
while ( 1 )
break ;
printf ( "%c", c ) ;
return 0;
File operation
fputc functions is used to write a character into a file. In a C program, we use fputc() function as below.
fputc(ch, fp);
where,
ch – character value
fp – file pointer
#include <stdio.h>
int main()
char ch;
FILE *fp1;
FILE *fp2;
/* Assume this test1.c file has some data.
ch = getc(fp1);
fputc(ch, fp2);
ch = getc(fp1);
fclose(fp1);
fclose(fp2);
return 0;
return 1;
File operation
fopen() Declaration: FILE *fopen (const char *filename, const char *mode)
fopen() function is used to open a file to perform operations such as reading, writing etc. In a C program,
we declare a file pointer and use fopen() as below. fopen() function creates a new file if the mentioned
file name does not exist.
FILE *fp;
Where,
filename – the actual file name with full path of the file.
mode – refers to the operation that will be performed on the file. Example: r, w, a, r+, w+ and a+. Please
refer below the description for these mode of operations.
fclose() function closes the file that is being pointed by file pointer fp. In a C program, we close a file as
below.
fclose (fp);
gets functions is used to read the string (sequence of characters) from keyboard input. In a C program,
we can read the string from standard input/keyboard as below.
gets (string);
fputs function writes string into a file pointed by fp. In a C program, we write string into a file as below.
# include <stdio.h>
# include <string.h>
int main( )
FILE *fp ;
char data[50];
fp = fopen("test.c", "w") ;
if ( fp == NULL )
return 1;
fputs(data, fp) ;
fputs("\n", fp) ;
fclose(fp) ;
return 0;
}
File operation
feof functions is used to find the end of a file. In a C program, we use feof() function as below.
feof(fp);
where,
fp – file pointer
# include <stdio.h>
int main( )
FILE *fp ;
char c ;
if ( fp == NULL )
return 1;
}
printf( "Reading the file test.c" ) ;
while ( 1 )
if( feof(fp) )
break ;
printf ( "%c", c ) ;
return 0;
ftell function is used to get current position of the file pointer. In a C program, we use ftell() as below.
ftell(fp);
#include <stdio.h>
int main ()
FILE *fp;
fp = fopen ("test.txt","w");
fprintf (fp, "%s %d", “Fresh2refresh”, 5);
rewind (fp); // It will move cursor position to the beginning of the file
fclose (fp);
return 0;
File operation
fseek() Declaration: int fseek(FILE *fp, long int offset, int whence)
fseek() function is used to move file pointer position to the given location.
where,
fp – file pointer
offset – Number of bytes/characters to be offset/moved from whence/the current file pointer position
whence – This is the current file pointer position from where offset is added. Below 3 constants are used
to specify this.
SEEK_SET SEEK_SET – It moves file pointer position to the beginning of the file.
#include <stdio.h>
int main ()
FILE *fp;
char data[60];
fp = fopen ("test.c","w");
fflush(data);
fflush(data);
fclose(fp);
return 0;
File operation
putw function is used to write an integer into a file. In a C program, we can write integer value in a file as
below.
putw(i, fp);
where
i – integer value
fp – file pointer
getw function reads an integer value from a file pointed by fp. In a C program, we can read integer value
from a file as below.
getw(fp);
#include <stdio.h>
int main ()
FILE *fp;
fp = fopen ("test.c","w");
putw(i,fp);
putw(j,fp);
putw(k,fp);
fclose(fp);
fp = fopen ("test.c","r");
while(getw(fp)!=EOF)
num= getw(fp);
fclose(fp);
return 0;
}
File operation
fflush() function is used to flush/clean the file or buffer. In a C program, we can use fflush() function as
below.
fflush(buffer);
#include <stdio.h>
int main()
char buf[50];
FILE *fp;
fp = fopen("test.txt", "r+");
if (fp)
fclose(fp);
return 0;
return 1;
}
File operation
sscanf() function is used to read formatted input from a string. In a C program, we can use sscanf()
function as below.
where,
buffer – buffer which has formatted data. Above is just for example only. Buffer can have any type of
formatted data within it.
/* sscanf example */
#include <stdio.h>
int main ()
int age;
return 0;
File operation
sprintf function is used to write formatted output to the string, In a C program, we use fgets function as
below.
where,
value – int variable, c – char variable and flt – float variable. There are for example only. You can use any
specifiers.
#include <stdio.h>
#include <string.h>
int main( )
int value = 50 ;
char c = 'Z' ;
return 0;
File operation
fputchar() function is used to write a character on standard output/screen. In a C program, we can use
fputchar function as below.
fputchar(char);
#include <stdio.h>
int main ()
char ch='a';
fputchar(c);
ch++;
return 0;