-1

I am trying to open a csv file in C++ using ifstream with a directory in the file path name. The file does reside in the specified directory location, but I observe an for the variable inFile when executing the code. My research up to this point says the code is correct, but something obviously is wrong. Any suggestions?

Thanks, KG

#include <string>
#include <fstream>
#include <iostream>
virtual void run()
{
    string file_dir = "/home/datafiles/";
    string csvFile = file_dir + "/myFile.csv";
 
    ifstream inFile;
    inFile.open("csvFile", ios::in);

    // file check to see if file is open
    if(!inFile.is_open()) {
        cout << "error while opening the file" << endl;
    }
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    `"/home/datafiles//myFile.csv"`?? Lose the `'/'` from `"/myFile.csv"` and just use `"myFile.csv"` Also do NOT quote `inFile.open("csvFile"...` just `inFile.open(csvfile, ...`. Better, don't hardcode filenames at all, just pass `csvfile` as a parameter to your function -- you shouldn't have to recompile just to read from a different filename... – David C. Rankin Jul 15 '21 at 00:17
  • I'm sure you understand it is `std::string`, `std::ifstream`, etc..., you must have `using namespace std;` somewhere else. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) – David C. Rankin Jul 15 '21 at 00:24
  • 2
    `inFile.open("csvFile", ios::in);` looks like it is opening a file named csvFile rather than using the `csvFile` variable. Remove the quotation marks to transform it from a string literal containing the text csvFile and into the intended `string` variable `csvFile`. – user4581301 Jul 15 '21 at 00:37
  • Side note: `ios::in` is implied with a `ifstream `. You can leave it out. – user4581301 Jul 15 '21 at 00:38

2 Answers2

0

I found the answer to my csv file opening problem, a colleague assisted.

@David - You suggested removing the double quotes in the "inFile.open" line of code. In addition to removing the double quotes, I also needed to add c_str(), which "returns a pointer to a null-terminated character array with data equivalent to those stored in the string," .data() also performs the same function (cppreference.com).

@user4581301 - I am also aware that ios::in is implied with a ifstream, only included it here as a reference; thanks.

The modified code is listed below:

#include <string>
#include <fstream>
#include <iostream>
virtual void run()
{
    string file_dir = "/home/datafiles/";
    string csvFile = file_dir + "/myFile.csv";
 
    ifstream inFile;
    inFile.open(csvFile.c_str(), ios::in);

    // file check to see if file is open
    if(!inFile.is_open()) {
        cout << "error while opening the file" << endl;
    }
}

Really appreciate all the help.

Enjoy, KG

-1

Is this what you're trying to do?

#include <iostream> // std::{ cout, endl }
#include <string>   // std::{ string, getline }
#include <fstream>  // std::ifstream

auto main() -> int {
  // Just to demonstrate. 
  // You want to use your real path instead of example.cpp
  auto file = std::ifstream("example.cpp");
  auto line = std::string();
  while ( std::getline(file, line) )
    std::cout << line << '\n';
  std::endl(std::cout);
}

Live example

viraltaco_
  • 814
  • 5
  • 14