C++Language FeatureLearn C++

How To Use fscanf In Your Own C++ Software

What is the fscanf function? How can I use fscanf in C++ software? Is there an example to the fscanf function in C++? What is the syntax of fscanf? Where can I find format specifiers for the fscanf function? Let’s answer these questions.

A little history of the fscanf function

fscanf function is an old C function (since C95) that reads data from files, scans, and formats input from a file stream. The fscanf scans a series of input fields one character at a time reading from a stream. Then each field is formatted according to a format specifier passed to fscanf in the format string pointed to by format. Finally, fscanf stores the formatted input at an address passed to it as an argument following format. The number of format specifiers and addresses must be the same as the number of input fields.

Syntax of fscanf (Since C99):

Note: For details on format specifiers, see scanf Format Specifiers.

fscanf can stop scanning a particular field before it reaches the normal end-of-field character (whitespace) or it can terminate entirely for a number of reasons. See scanf for a discussion of possible causes. If fscanf attempts to read at end-of-file, the return value is EOF. If no fields were stored, the return value is 0.

Return Value: fscanf returns the number of input fields successfully scanned, converted, and stored. The return value does not include scanned fields that were not stored.

Is there a simple example of how to use the fscanf function in C++?

Here is a example of fscanf,

What is a full example of the fscanf Function for C++ software?

Here is a full C++ Builder example, create a new Console VCL application in C++ Builder and modify as below.

Here is the output from the test.txt file which is also same as below,

This is a simple handle based file operation example. If you are looking to more examples about the Handle Based File Operations, please continue with this post below,

If you are looking some modern file operations, there are some Modern File operation examples in this post below,

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++17Introduction to C++Learn C++

How Do You Run A C++ Program?

CC++Introduction to C++Learn C++

Who Wrote The C Programming Language?

CC++Introduction to C++Language FeatureLearn C++

Is C A High Level Programming Language?

CC++Introduction to C++Learn C++

How To Run A C Program in Command Prompt