[英]C++ file open on windows always fails
我從另一個類似問題的答案中得到了這段代碼,但我不知道為什么這對我不起作用。 test.csv 和compiled.exe 文件在同一個文件夾中,但是沒有找到。 我嘗試了完整的系統路徑(“C:\Users\hhv\eclipse-workspace\oncemore\Debug\test.csv”),但仍然無法打開 csv。
所以我對正在發生的事情感到迷茫,因為我看過的所有其他示例看起來都應該有效。 例如: https://github.com/tpatil2/C-Programs/blob/master/RWcsv/rwcsv.cpp
#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" 轉義它們
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.