c++ - 在 C++ 中读取文件的问题
问题描述
我在用 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;
}
txt文件(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
解决方案
我从来没有见过你使用的语法,所以也许试试这个。替换ifstream inputFile(fileName);
为ifstream inputFile;
then 后跟inputFile.open(fileName);
.
推荐阅读
- python - 将 camelot 导入 python 时没有名为 cv2 的模块
- arrays - Google 电子表格 - 如何根据 2 列和其他列的条件计算唯一值?
- angular-material - 如何将自定义 svg 图标和常规角度材质图标添加到相同的 mat-icon 标签
- java - Runtime.freeMemory() 和公司或提前分配,以防止后期内存不足错误
- node.js - 一分钟后节点转失败
- matplotlib - 缺少 matplotlib api 文档
- javascript - 将标记从数据库 firebase 实时移动到 mapbox
- c# - 将 localhost 用于 ASP.NET Core API 时如何停止授权
- security - Azure DevOps 公共项目访问日志
- java - OptaPlanner - 将匹配限制为 x 数量