Because it is necessary to frequently access text files with mixed strings and values, it is a headache for me to effectively read such files. However, I learned the log [1] today. the solution is as follows.
The target file dat.txt is as follows:
Zzz 8 10.0 2.5 2.55
Llz 10 20.2 3.9 4.96
The read/write method is programmed as follows:
# Include <fstream>
# Include <iostream>
Using namespace STD;
Void main ()
{
Ifstream fin;
Char name [20];
Int inum;
Float fnum [3];
Fin. Open ("dat.txt"); // open the file to be read
If (Fin. Good () // you can check whether the image is successfully opened.
{
While (! Fin. EOF ())
{
Fin> name; // read the string
Fin> inum; // read integer values
For (INT I = 0; I <3; I ++)
{
Fin> fnum [I]; // read floating point cyclically
}
Cout <name <''<inum <'' <fnum [2] <Endl; // check whether the result is correct.
}
}
Else
{
Cout <"file can't open" <Endl;
}
Fin. Close (); // close the file
}
The running result is as follows:
Zzz 8 2.55
Llz 10 4.96
Press any key to continue
Reference address:Http://blog.sciencenet.cn/blog-481152-433178.html