fscanf in c

If you are searching for fscanf in c then read this blog post completely.

What is fscanf in c ?

fscanf in c used to read formatted data from a file. It works similar to scanf in c but it reads the data from a file.

It reads word from a file and returns EOF(End Of File) at the end of a file.

It is used as a input function to read content from file.

fscanf in c is found in standard input/output library that is stdio.h.

This function returns the number of fields converted and assigned. If error occurs it returns EOF.

Prototype

int fscanf(FILE *strm, const char *frmt,...);

Parameters for fscanf

Here FILE * is a file pointer that points to a file that identifies a stream. frmt is a c string that contains one or more items like whitespace character,non-whitespace character, and format specifiers.

Return value of fscanf

This function returns the number of input data matched and assigned or zero if error occurs.

Example programs for fscanf

#include <stdio.h>
int main(int argc, char *argv[]){
 FILE *fpointer;
 char ab[100];
 fpointer =fopen("abc.txt","r");
 while(fscanf(fpointer,"%s",ab)!=EOF)
 {
  printf("%s",ab);
 }
 return 0;
}
fscanf in c programming

Code explanation

The formatted input data is read from a file using fscanf function. The read data is printed on to the console.

fscanf() Man page


what are the differences between scanf() and fscanf()?

In c language scanf() function is used to take input from the user. Whereas fscanf() function is used to read the data from file.

Both functions reads characters , interprets them according to a format and stores the data in its argument.

The scanf() returns the number of input data that are scanned otherwise returns EOF if an error occurs. Whereas fscanf() function returns the number of characters that it converted and assigned otherwise EOF if an error occurs.

The scanf() function reads the formatted data from standard input such as a keyboard. Whereas fscanf() function reads the data from a file.

The scanf() function reads single value from the keyboard. Whereas fscanf in c reads one line from the file.

Prototypes are as below.

int scanf(const char *frmt,...);

int fscanf(FILE *strm,const char *frmt,...);

Have a look at the table below

fscanf in cscanf in c
Used to read the data from fileUsed to take input from the user
Stores data in its argument.Stores data in its argument.
Returns the number of characters converted and assigned otherwise EOF on errorReturns the number of input data that are scanned otherwise EOF on error
Reads the data from a fileReads the data from standard input
Reads one line from a fileReads single value at a time
int fscanf(FILE *strm,const char *frmt,...);int scanf(const char *frmt,...);

what is the difference between fscanf and getc ?

The getc() the function gets the next character as unsigned char from the specified stream and advances the pointer. Whereas fscanf in c reads the data from a file.

prototypes are as shown below

int getc(FILE *strm);

int fscanf(FILE *strm,const char *frmt,...);

Both functions needs pointer to advance. Both act as a input function.

The getc() the function returns the character read as an unsigned char cast to an int otherwise EOF on error. Whereas fscanf() function returns the number of characters that it converted and assigned otherwise EOF if an error occurs.

The getc() function takes single character from the stream. Whereas fscanf() function reads one line from a file.

Have a look at the table below.

fscanf in cgetc in c
Reads the data from a fileReads the next character as unsigned char from specified stream
int fscanf(FILE *strm,const char *frmt,...);
int getc(FILE *strm);
Returns the number of characters that it converted and assigned otherwise EOF if error occurs.Returns character read as unsigned char cast to an int otherwise error

What is the difference between fscanf and ftell()?

The ftell() function is used to get the total size of the file after moving the file pointer at the end of a file.fscanf function is used to read data from a file.

Prototypes are as shown below

long int ftell(FILE *strm);

int fscanf(FILE *strm,const char *frmt,...);

Both needs a pointer to advance.

The ftell() the function returns the current file position of the specified stream wrt the starting of a file. Whereas fscanf() function returns the number of characters that it converted and assigned otherwise EOF if an error occurs.

The ftell() function returns total size of a file. Whereas fscanf in c seeks single value at a time.

fscanf in cftell in c
Used to read data from a fileUsed to get the total size of a file after moving pointer to EOF
int fscanf(FILE *strm,const char *frmt,...);
long int ftell(FILE *strm);
Returns the number of characters that it converted and assigned otherwise EOF if error occurs.Returns the current file position of the specified stream wrt the starting of a file.
Seeks single value at a timeReturns total size of a file

What is the difference between fscanf and fprintf in c?

fscanf function reads formatted data from a stream. fprintf in c puts formatted data into a file.

Both functions requires a pointer which points to a file.

fprintf in c is used as output function.fscanf in c is used as input function.

As both are file handling i/o functions,they have same prototype.

fscanf the function returns the input data which are matched or zero on matching failure. whereas fprintf the function returns a total number of characters written or negative number on error.

fscanf in c removes all the whitespaces before reading the characters. Whereas fprintf the function does not remove any whitespace.

Look at the below table

fprintf in cfscanf in c
Writes formatted data to a fileReads formatted data from stream
Output functionInput function
int fprintf(FILE *strm,const char *formt,…);int fscanf(FILE * fpointer, conast char *frmt[argmt,…]);
Returns negative number on errorReturns zero on error
Does not remove any whitespacesRemoves all whitespaces before seek.

People also ask for

What is the use of fscanf in c?

It is used to read set of characters from a file.

What is the difference between fscanf and rewind function in c?

Rewind function is used to move file pointer position to the beginning of a file. Whereas fscanf function used to read formatted data from a file.

What is the difference between fscanf and fseek in c?

The fseek () function is used to move a file pointer to a given location in a file. Whereas fscanf() function is used to read formatted data from a file.

What is the difference between fscanf and fread/fwrite function in c?

The fread/fwrite functions are used for binary input/output files. Whereas fscanf() function is used to read formatted data from a file.

What are the errors in fscanf function if operations fails?

Here are some errors listed of fscanf function while operation fails
1.EILSEQ
2.EINVAL

How to read from keyboard using fscanf?

You can read from the input console using stdin as a file pointer. Look at the program which explains how to read from the input using fscanf in c.


How to read from keyboard using fscanf?

#include <stdio.h>
 int main(int argc, char *argv[])
{
  char str[100];
  printf("\n\n\tEnter a text\n\n\t");
  fscanf(stdin,"%[^\n]s",str);
  printf("\n\tYou entered:  %s\n\n",str);
  return 0;
}
program to read from input using fscanf in c