I've been knocking my head off this problem for a couple of days now. Have been trawling through the c++ references here and googling my way round a series of forums trying to find a solution.
I cant seem to open and read from a simple text file, no way, no how.
The stupid thing is that I know how to do this, I've written this functionality in c++ programs before, yet even when I copy pasted that code across and tweaked it for its new home it wouldnt co-operate.
Here's my massively cut down code (removed everything remotely irrelevant during bughunt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <fstream>
#include <io.h>
#include <iomanip>
void load()
{ ifstream myfile;
myfile.open("c:\test.txt", ifstream::in);
if(myfile.fail())
{
printf("Something went horribly wrong...\n";
}
while(myfile.good())
{
printf("We're in.\n");
/*Normally a bunch of io operations here, getline() or get() to move data from file to array*/
}
}
|
What this results in is the fail message everytime. For a long while I thought it was my various io operations causing a fail (such as trying to read in an empty final line), but this trimmed down version shows that it is just outright failing to even open the file. I've tried switching between stream types, tried ifstream and fstream. In desperation i tried ignoring (commenting away) the check for fail/bad bit but all this proved is that i never enter the file.good loop.
In case its relevant im trying to read in a txt file that contains a single line of integers (about 30) which im going to populate an array with.
Suggestions? Need more info? This is actually just one function in a ~2000 line program but I call it from the main method and have used some printf's to check its actually being called at the right time and everything. It just wont open the file.
Thanks, Celem