Formatted Text Mode
File can be in binary mode or text mode. Text mode deals with simple text and strings. Text mode can be in the form of unformatted text or in the form of formatted text/strings. We use the word formatted because records are stored in human readable format. These files can be viewed by a simple text editor and can be altered in the same way. C library has two API routines fscanf and fprintf. fscanf is to read formatted strings from the file and fprintf is to write formatted strings to a file. We will re-write our previous student database program with formatted string APIs.
Formatted Mode fprintf
fprintf takes two fixed arguments and can take variable additional arguments. First argument is the file pointer. Second argument is the format string. Additional arguments depends of formatting string. User has to provide the elements in the same sequence as the format string has been given. fprintf scans the formatting string and replaces all the %<element> with the text equivalent of the arguments given. This buffer then goes for writing to the device. It returns the number of bytes written to the files.
Formatted Mode fprintf C code
== Student Database Add records == Name : Student 1 Roll : 1 Std : 1 Record added.
Formatted Mode fprintf
fscanf takes similar arguments as fprintf. It also take two fixed arguments and can take variable additional arguments. First argument is the file pointer. Second argument is the format string. Additional arguments depends of formatting string. User has to provide the elements in the same sequence as the format string has been given. fscanf reads the buffer from file and scans input buffer as per formatting string and whenever it finds %<element> it converts the element and stores in the address given in the additional arguments. User has to provide the reference of the variables in the arguments in order to receive type objects from file. It returns the number of successful arguments converted successfully.
Formatted Mode fscanf C code
== Student Database Display records == Name : Student 1, Roll : 1, Std : 1
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Calculate weighted average