Professional Documents
Culture Documents
File Handling - Additional Functions From Stdio.h: Fscanf
File Handling - Additional Functions From Stdio.h: Fscanf
h
Here is a list of functions that we normally use for file handling
fscanf()
The C library function int fscanf(FILE *stream, const char *format, ...) reads formatted
input from a stream.
Declaration
#include <stdio.h>
#include <stdlib.h>
int main ()
int year;
FILE * fp;
rewind(fp);
fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
fclose(fp);
return(0);
Output will be
fprintf()
The C library function int fprintf(FILE *stream, const char *format, ...) sends formatted output to a stream.
Declaration
int fprintf(FILE *stream, const char *format, ...)
Parameters
stream − This is the pointer to a FILE object that identifies the stream.
format − This is the C string that contains the text to be written to the stream.
Example
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE * fp;
fp = fopen ("file.txt", "w+");
fclose(fp);
return(0);
Output
We are in 2012
fgetc()
The C library function int fgetc(FILE *stream) gets the next character (an unsigned char) from the specified stream
and advances the position indicator for the stream.
Declaration
Following is the declaration for fgetc() function.
int fgetc(FILE *stream)
Parameters
stream − This is the pointer to a FILE object that identifies the stream on which the operation is to be performed.
Return Value
This function returns the character read as an unsigned char cast to an int or EOF on end of file or error.
Example
The following example shows the usage of fgetc() function.
#include <stdio.h>
int main ()
FILE *fp;
int c;
int n = 0;
fp = fopen("file.txt","r");
if(fp == NULL)
return(-1);
do
c = fgetc(fp);
if( feof(fp) )
break ;
}
printf("%c", c);
while(1);
fclose(fp);
return(0);
The program assumes that we have a text file file.txt, which has the following content.
This file will be used as an input for our example program −
We are in 2012
Output
We are in 2012
fputc()
The C library function int fputc(int char, FILE *stream) writes a character (an unsigned char) specified by the
argument char to the specified stream and advances the position indicator for the stream.
Declaration
int fputc(int char, FILE *stream)
Parameters
char − This is the character to be written. This is passed as its int promotion.
stream − This is the pointer to a FILE object that identifies the stream where the character is to be written.
Return Value
If there are no errors, the same character that has been written is returned. If an error occurs, EOF is returned and the
error indicator is set.
Example
The following example shows the usage of fputc() function.
#include <stdio.h>
int main ()
{
FILE *fp;
int ch;
fp = fopen("file.txt", "w+");
for( ch = 33 ; ch <= 100; ch++ )
{
fputc(ch, fp);
}
fclose(fp);
return(0);
}
Output
On running the above program that will create a file file.txt in the current directory, which will have
following content −
!"#$%&'()*+,-
./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
fgets()
The C library function char *fgets(char *str, int n, FILE *stream) reads a line from the specified stream and stores it
into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the
end-of- file is reached, whichever comes first.
Declaration
char *fgets(char *str, int n, FILE *stream)
Parameters
str − This is the pointer to an array of chars where the string read is stored.
n − This is the maximum number of characters to be read (including the final null-character). Usually, the length of
the array passed as str is used.
stream − This is the pointer to a FILE object that identifies the stream where characters are read from.
Return Value
On success, the function returns the same str parameter. If the End-of- File is encountered and no characters have
been read, the contents of str remain unchanged and a null pointer is returned. If an error occurs, a null pointer is
returned.
Example
#include <stdio.h>
int main ()
{
FILE *fp;
char str[60];
fclose(fp);
return(0);
}
We have to have a text file file.txt, let us suppose the following content exists in it. This file will be
used as an input for our example program –
We are in 2012
fputs()
The C library function int fputs(const char *str, FILE *stream) writes a string to the specified stream up to but not
including the null character.
Declaration
int fputs(const char *str, FILE *stream)
Parameters
str − This is an array containing the null-terminated sequence of characters to be written.
stream − This is the pointer to a FILE object that identifies the stream where the string is to be
written.
Return Value
This function returns a non-negative value, or else on error it returns EOF.
Example
The following example shows the usage of fputs() function.
#include <stdio.h>
int main ()
{
FILE *fp;
fp = fopen("file.txt", "w+");
Output
This will create a file file.txt with the following content −
#include <stdio.h>
#include <string.h>
int main ()
{
FILE *fp;
char c[] = "this is Internshala";
char buffer[100];
fclose(fp);
return(0);
Let us compile and run the above program that will create a file file.txt and write a content “this is Internshala”.
After that, we use fseek() function to reset writing pointer to the beginning of the file and prepare the file content.
Then fread() function is used for reading the same and the content is printed on the display.
Output
this is Internshala
fwrite()