tags:

views:

59

answers:

2

When I use the std::ifstream to open a file that has been written in dos format, the ifstream does not seem to be able to open the file correctly since when I call good() on the stream afterwards it fails (returns false). I tried opening the file in binary mode as well as the default "in" mode and neither worked. If I convert the file to unix using dos2unix, everything works fine.

The goal behind being able to do this is that I want to be able to read a file and parse it, but I cannot guarantee that the file has not been saved in dos (Windows) or unix (Linux) format. Ideally, I would like to be able to use the ifstream.

Any suggestions?

Thanks!

+1  A: 

The file format will NOT affect your ability to open it.

It is more likely that your path is not correct.

Martin York
Is the post about Ctrl+M characters? the post also mentions that doing dos2unix works fine. So it's not about path I guess. Highlighted the post appropriately
Chubsdad
Ctrl-M character will not affect the way ifstream's work. It is more likely that the filename was changed from a bad name to a good name. Is the file system case sensitive?
Martin York
+1  A: 

It seems extremely unlikely that GCC would do such a thing.

I suggest doing my_ifstream.exceptions( ios::failbit | ios::badbit ); before opening it, and running in the debugger. Then you can see where it ceased to be good.

Also, opening the file in binary mode (ios::in | ios::binary) should eliminate any possibility of the implementation being choosy over the contents of the file.

Potatoswatter