#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line,p1,p2,p3;
ifstream myfile ("infile.txt");
if (myfile.is_open())
{
while (myfile.good())
{
/*
myfile >> p1 >> p2 >> p3 ;
cout << "p1 : " << p1 << " p2 : " << p2 << " p3 : " << p3 << endl;
*/
getline (myfile,line);
cout << "line : " << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
INFILE.TXT
name
param1 = xyz
param2 = 99
param3
param_abc = 0.1
june 2012
輸出
line : name
line : param1 = xyz
line : param2 = 99
line : param3
line : param_abc = 0.1
line : june 2012
line :
我想搜索參數2,並打印其值即99
你有沒有擡頭'的std :: map'和/或'的std :: multimap' ? –
[請不要使用'.good()'作爲循環條件。](http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line)它通常會產生錯誤的代碼(就像這裏所做的那樣)。在你的情況下,使用'getline'的返回值:while(std :: getline(myfile,line)){std :: cout <<「line:」<< line <<「\ n」; }' –