Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - c++, the last line of the txt file contains data: Hello, good, zzz001 4 1000. How to read out the integer data of 1000 and assign it to another variable?
c++, the last line of the txt file contains data: Hello, good, zzz001 4 1000. How to read out the integer data of 1000 and assign it to another variable?

In the Windows operating system, files are divided into streaming files and record files. The latter can be read and written randomly, while the former can only be read sequentially. .txt is a streaming file and must be read sequentially, that is, the last line must be read before the 1000 in the last line can be read.

The c++ code is as follows: //Use the most basic stream operation to complete first:

//The file is assumed to be 1.txt

ifstream?in("1.txt");//Want to #include

string? s;

while(getline(in,s));

//At this point, you can operate in a variety of ways

//Method 1. String stream operations, directly saved to integer variables

string?temp,int?num;

istringstream?sin(s);

for(int ?i(1);sin>>temp&i<=4;++i);

sin>>num;//The value stored in num is 1000

//Method 2 .c++ function call, substr(), a member of the string class, check the usage yourself, it is also available in Baidu Encyclopedia

basic_string??s_num?=?s.substr?(?17?,?4 ?);

//Other methods: You can also use vector arrays, etc. Try it yourself.