Site icon Learn 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):

[crayon-6763284b0cd46405866546/]

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,

[crayon-6763284b0cd4f236661387/]

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.

[crayon-6763284b0cd50768006813/]

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

[crayon-6763284b0cd51962542630/]

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.

Exit mobile version