我有一个名为file.txt的文件,它有这样的结构:
owner_name : first_last_name
filesize : 1000
is_legal_file : yes
date : someDate
.
.
.
我想在fileSize中获取值。 (本例中为1000。)
如何获取此信息?
答案 0 :(得分:1)
答案 1 :(得分:0)
除了strtok之外的另一个简单方法是做while (infile >> myString)
。只需弄清楚你想要的值的数量并将其取出。
std::string myString;
ifstream infile("yourFile.txt");
while (infile >> myString)
{
//Do an if-statement to only select the value you want.
//Leaving this for you since I think it's homework
}
答案 2 :(得分:0)
sstream
#include <iostream>
#include <sstream>
#include <string>
int main() {
using namespace std;
for (string line; getline(cin, line); ) {
istringstream ss(line);
string name;
ss >> name; // assume spaces between all elements in the line
if (name == "filesize") {
string sep;
int filesize = -1;
ss >> sep >> filesize;
if (sep == ":" && ss) {
cout << filesize << endl;
break;
}
}
}
}
1000