Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
C++ Tutorial
C++ Language Tutorial - Object Oriented Programing - OOPs History of C++
C++ Language Tutorial - Object Oriented Programing - OOPs Structure of C++
C++ Language Tutorial - Object Oriented Programing - OOPs C++ Datatypes
C++ Language Tutorial - Object Oriented Programing - OOPs Variables
C++ Language Tutorial - Object Oriented Programing - OOPs Constants
C++ Language Tutorial - Object Oriented Programing - OOPs Operators
C++ Language Tutorial - Object Oriented Programing - OOPs Control Structures
C++ Language Tutorial - Object Oriented Programing - OOPs Arrays
C++ Language Tutorial - Object Oriented Programing - OOPs Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Class
C++ Language Tutorial - Object Oriented Programing - OOPs Predefined Functions
C++ Language Tutorial - Object Oriented Programing - OOPs I/O Functions
C++ Language Tutorial - Object Oriented Programing - OOPs String, Character Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Mathematical Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Time Date Functions
C++ Language Tutorial - Object Oriented Programing - OOPs Dynamic Allocation
C++ Language Tutorial - Object Oriented Programing - OOPs Utility Functions
C++ Language Tutorial - Object Oriented Programing - OOPs OOP's Concept
C++ Language Tutorial - Object Oriented Programing - OOPs Special Topics
C++ Language Tutorial - Object Oriented Programing - OOPs Type casting
C++ Language Tutorial - Object Oriented Programing - OOPs Feedback
C++ Language Tutorial - Object Oriented Programing - OOPs Ask Your Doubts
 




fscanf() - I/O Function


Tutorials Cpp

Topic

How is "fscanf()" used in C++?



Explanation

fscanf() is an I/O function similar to scanf but this function reads information from a stream. The function returns the number of items read on success, it can be zero if a matching failure occurs. If an input failure occurs before reading data could result in EOF.

Syntax:
    int fscanf( FILE * stream, const char *format,...);

In the above syntax in the format parameter the function discards the whitespace.All the non-whitespace characters except "%" are skipped, then the next character is read. The "%" indicates a format specifier. The following table lists the format specifier used with "fscanf" function.

Type Description Argument
c Used to read a single character, if the width is more than 1 the function reads all the specified width of characters and stores them in successive locations of the array passed as argument. char *
d Represents a decimal integer that mey be preceeded with +/ - sign. int *
e,E,f,g,G Used to represent a floating point number containing a decimal point that may have a +/- sign with a e or E character. float *
o Specifies an octal value int *
s This format type is used to read string of characters until a whitespace is found. char *
u Unsigned decimal integer unsigned int *
x,X Represents a hexadecimal integer. int *

Example:

   #include <stdio.h>
   int main() 
     {
       FILE *fp;
       char name[100];
       fp = fopen("fscanfeg.txt", "r");
       fscanf(fp, "%s", name);
       printf("%s\n", name);
       fclose(fp);
       return 0;
     }

Result:
    Hscripts

In the above example the "fscanf()" is used to read the text "Hscripts" from the text file.










Note
C++ is one of the most used programming languages in the world. Also known as "C with Classes". Hope you enjoy this tutorial. Do send your feedback or suggestions on this C++ tutorial. This is a copyright content.

Other Links

web hosting