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.

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?

C++C++17Code SnippetLearn C++

How To Convert Char Array String To Unicode String Correctly In C++?

Artificial Intelligence TechC++Language FeatureLearn C++

How To Develop A Simple Hopfield Network In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

Learn Default Constructors Of Classes In Modern C++