Filestream headaches

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
Last edited on
Line 8 should be myfile.open("c:\\test.txt", ifstream::in); // Note '\\'

"\t" is the tab. No way to open such a file...
mmm well spotted, thanks. Cant believe I missed the escape. However im still getting the failbit set and the corresponding message. myfile.good() still evaluates false (0)
Last edited on
Or you can write "c:/test.txt"
I took the above code snippet and created a new project using my trusty old Bloodshed Dev-C++. Set up a console app and ran the code with a demo file........everything worked just fine. Which would suggest its something with the environment the code now finds itself in :(

Incidentally its running as a Win32 app with an attached console (its part of a 3d game) and the method is called just before my main gameloop. Im gonna move the method call about a bit and see if I can identify the conflict going on.......it could just as likely be some stupidity stemming from my IDE (MSVC++ 2008 Exp)
Topic archived. No new replies allowed.