1

I got this code from another answer to a similar question, but I can't figure out why this is not working for me. The test.csv is in the same folder as the compiled .exe file, but it doesn't find it. I tried the full system path ("C:\Users\hhv\eclipse-workspace\oncemore\Debug\test.csv") but it still fails to open the csv.

cpp file open fails

So I am at a lose over what's going on because every other example I have looked at looked like this should be working. Ex: 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;


}

D.Zou
  • 761
  • 8
  • 25
  • 2
    What's the working directory of your program? It could very well be different than the executable directory. It's even quite possible that running the program through the IDE sets the working directory to something other than the executable directory (e.g., the project directory). – chris May 25 '20 at 19:23
  • What exactly is the error / exception you are getting? – sanitizedUser May 25 '20 at 19:24
  • @sanitizedUser the error is "error while opening symbol file". Which is what it is suppose to print if it can't open the file. – D.Zou May 25 '20 at 19:26
  • @D.Zou No, I mean the system error. Try printing out the error with help from this [answer](https://stackoverflow.com/a/17338934/10732434). – sanitizedUser May 25 '20 at 19:27
  • most probably access issues with your user. what is the access mod of file ( ls -la )? – Abhiroj Panwar May 25 '20 at 19:29
  • As @crist suggested there can be problem with file location. Try using absolute path, e.g. "C:\\Users\\hhv\\eclipse-workspace\\oncemore\\Debug\\test.csv" – stefan.gal May 25 '20 at 19:39

1 Answers1

1

The path is relative to the current working directory (where the program is executed), not to the source file. Also when you use backslashes in the file path you must escape them like so "C:\\Users\\hhv\\eclipse-workspace\\oncemore\\Debug\\test.csv"

P. Milev
  • 374
  • 2
  • 12
  • thx this worked. But if the path is relative to the working directory, and if I have the .exe and the test.csv in the same folder, why did it still not find my file? – D.Zou May 25 '20 at 19:37
  • In eclipse the default working directory is the workspace directory. In your case I think that's ```oncemore/``` so try to use "Debug\\test.csv" instead. – P. Milev May 26 '20 at 08:19