我的問題是,我試圖從文本文件中輸入字符,字符串和int。我知道如何使用getline()進行輸入,但是在使用get line()函數後,不再需要在字符串後輸入其餘整數。我的問題是,我怎樣才能輸入一個字符,然後輸入一個字符串(帶空格)後跟3個整數?如何從文本文件中讀取具有一定長度的空格的輸入字符串C++
的data.txt看起來像這樣
a New York 5 7 9
b Virginia 10 2 5
c Los Angeles 25 15 6
這裏是我的代碼:
int main()
{
string city;
double price;
int amt1, amt2, amt3;
char orderStatus;
ifstream warehouse;
ofstream output;
warehouse.open("data.txt");
output.open("dataOut.txt");
while (warehouse.good())
{
warehouse >> orderStatus;
output << orderStatus << "\t";
getline(warehouse, city, '\t');
//warehouse >> city;
output << city << endl;
//warehouse >> amt1;
//output << amt1 << "\t";
//warehouse >> amt2;
//output << amt2 << "\t";
//warehouse >> amt3;
//output << amt3;
}
warehouse.close();
output.close();
return 0;
}
感謝您的很多需要幫助。
其中一種方法是在獲取行後標記字符串。 – Mahesh
同意@Mahesh。那樣做了幾次之前。問題是,當你的「int」來自一個文本文件時,它是一個ASCII字符。這意味着它實際上是一個char/string,它需要被轉換爲int。 – guitarflow