I am attempting to read from a .csv file in c++. After calling myfile.open(file), is_open returns true, but getline is only returning empty strings.
I have attempted using vectors to read the lines and then writing the vector data to a variable to read, but that has also returned only empty strings.
std::ifstream csvFile;
std::string line = "!", temp= "...";
csvFile.open("file.csv");
if(csvFile.is_open()) {
std::cout << "open\n";
std::cout << line << "\n";
if(getline(csvFile, line)) {
std:: cout << line << "\n";
} else {
std::cout << temp << "\n";
}
}
else {
std:: cout << "not opening\n";
}
std:: cout << line;
My output is as follows after running.
[ctest] open
[ctest] !
[ctest] ...
[ctest]
As shown, the getline() return only an empty string, although the file itself is not empty.
These are the first 20 or so lines of the csv file, and I have made sure that the file is in the current working directory.
Alpha002
16:55:54 13/6/2019
428,1.61,-1.31,-0.13,0,0
448,1.61,-1.47,-0.13,0,0
468,1.68,-1.07,-0.44,0,0
488,1.61,-1.39,-0.76,0,0
508,1.61,-1.47,-0.68,0,0
3528,1.61,-1.55,-0.36,0,0
3548,1.61,-1.31,-0.28,0,0
3568,1.68,-1.15,-0.36,0,0
3588,1.68,-1.63,-0.76,0,0
3608,1.68,-0.76,-0.68,0,0
3628,1.68,-1.15,-0.21,0,0
3648,1.68,-0.76,-0.28,0,0
3668,1.68,-1.39,-0.13,0,0
3688,1.68,-1.07,-0.21,0,0
3708,1.61,-1.47,0.03,0,0
I am not sure how to proceed from here, as i cannot find any issues apart from that, any advice is appreciated!
I found the issue, something is wrong with the file itself I was using. I tried reading from another file using the same methods, and it worked without issues. Thank you to everyone who commented trying to help!
I have a text file that contains several words, all separated by spaces. I'm trying to read the file and then put it into an array, so that each word is a separate value in said array. I'm using this code, but when I run my program, it doesn't display anything (like it should.)
ifstream file ("words.txt");
if(file.is_open())
{
string wordArray[100];
for(int i = 0; i < 100; ++i)
{
file >> wordArray[i];
cout << i;
}
cout << "File is open.";
}
Nothing displays at all. I'm doing this in a void function, which isn't being passed anything currently, but I don't think that has anything to do with it. The code should at least display "File is open" or any number from 1 to 100, but I don't get anything. I don't understand why this isn't working, as I'm including iostream, string, fstream, iomanip, and sstream. If there's something simple I'm overlooking, please let me know.
Well, your program probably isn't passing the statement in the if condition.
Try adding this to test your file is opening correctly:
if(file){
// do all the file inputs
}
else{
std::cerr << "could not open file words.txt" << std::endl;
}
my partner wrote a bunch of code for one of my projects in a text editor, when i run the code it works perfectly.....
now i have copy and pasted all the code into qt creator, and im having an issue
stringstream ss;
string line;
ifstream myfile;
myfile.open("Instructors.txt");
if (myfile.is_open()){
while (getline(myfile,line)){
ss << line << ", ";
}
myfile.close();
}
else cout << "bad open" << endl;
above is the part fo my code that is having the issue, i can assure you all Instructors.txt is indeed in in the correct file, but everytime our code reaches this point imstead of opening the file i get thrown to the else "bad open" why would this be?
It's hard to say what it may be without any error code, what you can do is to improve your error message with something more meaningful (for you and for your customers):
else cout << "Error opening file: " << strerror(errno) << endl;
strerror (see reference) function returns a string for a given error code captured in by errno macro.
Otherwise you can do it much more C++ish using exceptions: first enable them for your stream:
myfile.exceptions(ifstream::failbit | ifstream::badbit);
Then catch them, all together is:
try
{
ifstream myfile("Instructors.txt");
myfile.exceptions(ifstream::failbit | ifstream::badbit);
while (getline(myfile, line))
{
ss << line << ", ";
}
myfile.close();
}
catch (ifstream::failure e)
{
cout << e.what() << endl;
}
Try to rewrite file name, may be it contains characters from different encodings.
double check the working directory, chances are it is in the build folder (where the executable gets dropped)
in QtCreator you can fix this by going to projects and selecting run; there you will be able to set the working directory
I am working on a C++ HW assignment dealing with file I/O. I've never done this before, and is probably pretty obvious, but I would like to ask a quick question about including a path to my output and input files. In the example I am storing it in the c drive of a temp directory, though it is not showing up anywhere. Am I going about this wrong, or am I not looking in the right place in my c drive? Thx for the help, here a little copy of my code
ifstream in;
ofstream out;
in.open("c:/temp/gradeBook.txt");
if(in.fail());
{
cout << "Input file opening failed.\n";
exit(1);
}
out.open("c:/temp/gradeBook2.txt");
if (out.fail());
{
cout << "Out put file opening failed. \n";
exit(1);
}
cout << "A new gradefile has been comepleted" << endl;
calculate(in,out);
out.close();
in.close();
system("PAUSE");
return 0;
}
void getBookData(bookType books[], int& noOfBooks)
{
ifstream infile;
string file = "bookData.txt";
infile.open(file.c_str());
if (infile.fail()) {
cout << "No file found!" << endl;
infile.clear();
}
while (true) {
string line;
getline(infile, line, '\r');
if (infile.fail()) {
break;
}
cout << "Line: " << line << endl;
}
infile.close();
}
I've tried putting the file in every location I can think of, but somehow it's not loading in. Or, more likely, I'm doing something else wrong. This isn't anything like what the end result of my code is supposed to be like, right now I'm just trying to read out my file line by line.
I guess you really need help debugging why this is happening to you.
Try adding some more code to your routine to help you determine what is going on. One thing to try is to call getcwd.
#include <unistd.h>
...
char buf[PATH_MAX];
std::cout << "cwd: " << getcwd(buf, sizeof(buf)) << std::endl;
...
This should report to you where your program thinks it is running from.
Start with that first, and I am guessing the next steps will become obvious to you.