gpt4 book ai didi

c++ - 在Windows上打开的C++文件始终失败

转载 作者:行者123 更新时间:2023-12-02 10:15:40 26 4
gpt4 key购买 nike

我从另一个类似问题的答案中获得了此代码,但我不知道为什么这对我不起作用。 test.csv与已编译的.exe文件位于同一文件夹中,但找不到该文件。我尝试了完整的系统路径(“C:\ Users \ hhv \ eclipse-workspace \ oncemore \ Debug \ test.csv”),但仍然无法打开csv。

cpp file open fails

所以我对正在发生的事情不知所措,因为我看过的所有其他示例看起来都应该起作用。
例如:
https://github.com/tpatil2/C-Programs/blob/master/RWcsv/rwcsv.cpp

c++ reading csv file



#include <iostream>
#include <fstream>
#include <sstream>

#include <string>
#include <vector>
#include "load_symbol.h"
using namespace std;


bool load_symbols(){


string line; /* string to hold each line */
vector<vector<int>> array; /* vector of vector<int> for 2d array */

ifstream f ("test.csv"); /* open file */
if (!f.is_open()) { /* validate file open for reading */
perror ("error while opening symbol file ");
return false;
}

while (getline (f, line)) { /* read each line */
string val; /* string to hold value */
vector<int> row; /* vector for row of values */
stringstream s (line); /* stringstream to parse csv */
while (getline (s, val, ',')) /* for each value */
row.push_back (stoi(val)); /* convert to int, add to row */
array.push_back (row); /* add row to array */
}
f.close();

cout << "complete array\n\n";
for (auto& row : array) { /* iterate over rows */
for (auto& val : row) /* iterate over vals */
cout << val << " "; /* output value */
cout << "\n"; /* tidy up with '\n' */
}


return true;


}

最佳答案

该路径是相对于当前工作目录(执行程序的位置)的,而不是相对于源文件的。同样,当您在文件路径中使用反斜杠时,也必须像“C:\\ Users \\ hhv \\ eclipse-workspace \\ oncemore \\ Debug \\ test.csv”那样对它们进行转义。

关于c++ - 在Windows上打开的C++文件始终失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62009270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com