【发布时间】:2021-03-01 03:52:44
【问题描述】:
我在读取 C++ 文件时遇到问题。将包括相关代码和我的文件示例。提前谢谢你。
string fileName;
cout << "Enter file name: ";
cin >> fileName;
ifstream inputFile(fileName);
if (inputFile.is_open()) {
getline(inputFile, line);
pNum = stoi(line);
int i = 0;
while (getline(inputFile, line)) {
double x = stod(line.substr(0, line.find(" ")));
double y = stod(line.substr(line.find(" ")));
Point pTemp;
pTemp.x = x;
pTemp.y = y;
p.push_back(pTemp);
i++;
cout << "File is open" << endl;
}
inputFile.close();
}
else {
cout << "Problem reading input file" << endl;
}
文本文件 (file7.txt)
5.63585 0.0125126
8.08741 1.93304
4.79873 5.85009
8.95962 3.50291
7.46605 8.2284
8.58943 1.74108
5.13535 7.10501
【问题讨论】:
-
这段代码有什么问题?它会产生某种错误吗?
-
@SilvioMayolo,即使我输入名称“file7.txt”,它也会跳到“读取输入文件时出现问题”。我一步一步走了,我不确定它有什么问题。
-
我的猜测是,当前工作目录不是你想的那样,文件
file7.txt不在里面。 -
我对@IgorTandetnik 感到很困惑,我对我一直在使用的 IDE 不熟悉
标签: c++ algorithm data-structures