0
我想提取單個變量中文件行的每個單詞。從C++中的文件中提取重複的數據
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc,char*argv[]){
ifstream myfile;
myfile.open("position.txt",ios::in);
string line;
while(getline(myfile,line)){
stringstream linestream(line);
string id;
int idNumber,posX,posY,frameNum;
linestream >> std::skipws;
linestream >> id >> idNumber >> posX >> posY >> frameNum;
cout << "idNumber" << idNumber << endl;
cout << "posX" << posX << endl;
cout << "posY" << posY << endl;
cout << "frameNum" << frameNum << "\n \n";
}
myfile.close();
return 0;
}
position.txt:
id: 1 263 138 0
id: 2 3 53 41
id: 3 3 40 112
id: 3 37 40 129
但我在這樣的輸出,可變重複了兩次:
idNumber1
posX263
posY138
frameNum0
idNumber1
posX263
posY138
frameNum0
idNumber2
posX3
posY53
frameNum41
idNumber2
posX3
posY53
frameNum41
我不明白什麼是錯在我的程序,可以在任何有人告訴我這個錯誤嗎?
謝謝你,它工作得很好。 – para