【发布时间】:2016-05-04 09:25:26
【问题描述】:
目前正在尝试从文本文件中存储一些信息,文本文件的格式为数字/文本/文本/数字,我想将它们分别保存为int、string、string和float。在文件中,它们实际上由 / 分隔,因此文本文件的示例可能类似于 173/hello/goodbye/13.4
这是我目前的代码
int main(){
string menuname;
int a;
float d;
string b;
string c;
bool error = false;
ifstream openFile;
do{
cout << "Please enter the name of the menu you would like to open: ";
cin >> menuname;
menuname += ".txt";
openFile.open(menuname.c_str());
if(openFile.fail()){
cerr << "Unable to open file, please re-enter the name.\n";
error = true;
}
}while(error == true);
openFile >> a;
openFile >> b;
openFile >> c;
openFile >> d;
cout << itemno << category;
}
我不是 100% 确定导致问题的原因,但我感觉这可能与字符串有关,好像我只是使用 openFile >> a; 和下一个 3 它正确显示,但在添加 @ 之后987654324@it 显示整行,而不仅仅是旁边的单词/短语。
【问题讨论】:
-
>> 运算符用空格分隔输入。您不希望它被空格分隔,因此您需要使用其他东西。有很多选择:见stackoverflow.com/questions/7302996/…