【发布时间】:2012-06-18 23:26:38
【问题描述】:
我的问题是我试图从文本文件中输入 char、string 然后 int。我知道如何使用 getline() 进行输入,但是在使用 getline() 函数之后,不再有输入字符串后面其余整数的选项。我的问题是,如何输入一个字符,然后是一个字符串(带空格),后跟 3 个整数?
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。之前做过几次都是这样。问题是您的“int”来自文本文件时是一个 ASCII 字符。这意味着它实际上是一个需要转换为 int 的 char/string。
标签: c++