ifstream with VC++ 2010

Hi,

Really obvious question I hope. Had a look around but I guess it's such an obvious thing nobody else in the world could get this wrong!

I did a fair bit of stuff in C++ about a year ago and am now getting back into it a little. Believe it or not I can't get a basic ifstream to work!

Here is my code snippet:
1
2
3
4
5
6
7
8
9
10
11
		string urlToTest;
		ifstream tests("c:\test\tests.txt");
		//tests.open("tests");
		//char* testInp = argv[1];
		//ifstream tests("c:\test\tests.txt");
		
		while(tests.good())
		{

			getline(tests, urlToTest);
}


Obviously this isn't the full thing - the thing compiles fine. The problem is the while statement "fails" every time - and before anyone suggests it I am certain the file exists; I have also tried files in the working / current directory.

The reason why I have put VC++ 2010 in the Title is that in VC++ 2005 which I used before, this worked fine - it is actually old code. I had to do a bit of messing around getting some stuff to work from 2005 to 2010 - I just can't work this out.

Anyone had a similar problem, or can help??

Thanks
on line two replace c:\test\tests.txt with c:\\test\\test.txt

to explain why, you should note that '\' signifies the opening of an escape sequence. Such as '\n' to input a new line or '\0' for NULL.
Last edited on
Thanks for the quick reply. That hasn't actually worked though.

I have just tried creating a file first using ofstream and then reading that in and it has worked?!?!?!?! It doesn't seem to work on "existing" files...

Does anyone know why - windows security access levels are fine, so i've no idea.
can you give me an exact error message?
Sorry no error - ive just debugged and it skips passed the while statement...
how are you making the file?

unless something is wrong with other parts of your code, that SHOULD be working.

EDIT:

instead of checking if the status bit is good, I'd use while(!tests.eof())

That checks for the bit being set when the file is at its end.
Last edited on
Topic archived. No new replies allowed.