Fscanf is a c api for reading text files. It features formatting and reading of file content.
1 FILE* pf = fopen("c:\\hello.txt", "r");
2 if (NULL==pf)
3 return;
4
5 char cstr[256];
6 fscanf(pf, "%s", cstr);
7 fclose(pf);
Fscanf uses spaces, tabs, and carriage returns to separate different words, making it easier to use.
Fscanf is encapsulated to search for target characters, read strings, integers, and double-precision floating point numbers.
1 #pragma once
2 #pragma warning (disable:4996)
3
4 inline bool HitFlag(FILE* pf, const char* flag)
5 {
6 char chs[256];
7 while (!feof(pf))
8 {
9 fscanf(pf, "%s", chs);
10 if (0==strcmp(flag, chs))
11 return true;
12 }
13
14 return false;
15 }
16
17 inline std::string ReadStrVal(FILE* pf)
18 {
19 char chs[256];
20 fscanf(pf, "%s", chs);
21 return std::string(chs);
22 }
23
24 inline int ReadIntVal(FILE* pf)
25 {
26 int iVal;
27 fscanf(pf, "%d", &iVal);
28 return iVal;
29 }
30
31 inline double ReadDblVal(FILE* pf)
32 {
33 double fVal;
34 fscanf(pf, "%lf", &fVal);
35 return fVal;
36 }