I have written some code in c++. It reads in data from a CSV file and then simply prints the second line to the screen:
vector<string> readCsvFileContent()
{
vector<string> buffer;
try {
ifstream inputFile;
string line;
inputFile.open("Input.csv", static_cast<std::ios::openmode>(std::ios::in) );
while (getline(inputFile,line)) {
buffer.push_back(line);
}
inputFile.close();
}
catch (ifstream::failure e) {
cout<<"No file read"<<endl;
throw e;
}
return buffer;
}
This function is called as follows :
cout << "reading from file" << endl;
vector<string> inputData = readCsvFileContent();
cout << inputData.size() << endl;
cout << inputData[1] << endl;
When it runs in debug it displays what it should:
[ 50%] Building CXX object src/CMakeFiles/version1.dir/version1.cc.o
Linking CXX executable version1
[ 50%] Built target version1
[100%] Generating House1.csv
reading from file
322274
"2014-07-01 00:00:06",155,0,0,0,NULL,0,0,0,0,NULL
[100%] Built target process_csv
But when I run my code I get:
reading from file
0
Segmentation fault (core dumped)
open(file,mode)inputData[1]has undefined behaviour. So, the problem is that either the file is empty, doesn't exist or isn't readable. Perhaps you're running your debug and release builds differently? Maybe they have different working directories and only debug actually has the file? Perhaps the file exists in the release directory with wrong permissions. Your code really should check that it has actually opened a (non-empty) file.