Sign in to follow this  
Lode

file loading with ifstream failing with VS2005

Recommended Posts

I use the following code to load a file:
void loadFile(std::vector<unsigned char>& buffer, const std::string& filename)
{
  std::ifstream file(filename.c_str(), std::ios::in|std::ios::binary|std::ios::ate);

  //get filesize
  if(!file.seekg(0, std::ios::end).good()) return;
  std::streamsize size = file.tellg();
  if(!file.seekg(0, std::ios::beg).good()) return;
  size -= file.tellg();

  //read contents of the file into the vector
  buffer.resize(size);
  file.read((char*)(&buffer[0]), size);
}




It works when compiling with g++, and also with VS2005 in debug mode and sometimes in release mode. However in VS2005, when running in release mode from within VS2005, then it faile: The first line if(!file.seekg(0, std::ios::end).good()) return;, doesn't return "good" and it stops loading the file. Do you have any idea why this is happening, if there's something wrong with the loadFile function that can be fixed to solve this? The file I try to load with it exists and is in the folder of the Release executable (and in the folder with the Debug executable too). What IS the proper way to load a file with an ifstream into an std::vector, and does VS2005 support this way? I'd like to have standard C++ code to load a file into an std::vector that works in any sane standard C++ compiler, including Microsofts.

Share this post


Link to post
Share on other sites
The working directory when running from within Visual studio is where the project is located, not where the executable is located. Put your files in the project directory instead.

Reason is you shouldn't have to use absolute paths into your project directories, or multiple copies of your files depending on settings (debug vs. release, just to mention the default settings).

Share this post


Link to post
Share on other sites
Thanks, that worked indeed, and the other problems were related to the vector having size 0 after loading the file and &vector[0] then not working in VS2005 (unlike g++).

There is another slight problem now though: When declaring the ifstream (first line in the function of the code in my previous post), it gives a first chance exception. Is there something that can be done about this? Or does VS2005 try to access address 0x00000000 on purpose when creating an std::ifstream and should I not care?

Share this post


Link to post
Share on other sites
Quote:
Original post by Lode
Thanks, that worked indeed, and the other problems were related to the vector having size 0 after loading the file and &vector[0] then not working in VS2005 (unlike g++).

Appears to behave correct != works. If the vector is of size zero, then accessing the first element is an error not matter how you look at it. If the error is caught or not is another question.
Quote:
Original post by Lode
There is another slight problem now though: When declaring the ifstream (first line in the function of the code in my previous post), it gives a first chance exception. Is there something that can be done about this? Or does VS2005 try to access address 0x00000000 on purpose when creating an std::ifstream and should I not care?

What would the desired porpose of accessing an invalid address be when opening a file? If you get an exception, then you should most definitely not ignore the problem. Only thing I can think of in that particular line is with the filename string. Is it a valid string?

Share this post


Link to post
Share on other sites
Quote:
Original post by Brother Bob
What would the desired porpose of accessing an invalid address be when opening a file? If you get an exception, then you should most definitely not ignore the problem. Only thing I can think of in that particular line is with the filename string. Is it a valid string?


Yeah, it's valid, in the debugger it shows its contents and it's the name of an existing file in the right directory, and the program really opens the file and does the correct stuff to it.

Is maybe one of the modifiers (std::ios::in|std::ios::binary|std::ios::ate) wrong?

Share this post


Link to post
Share on other sites
I also noticed something strange in VC 2005. I previously compiled this code with MinGW and it worked without a hitch, but in VC it always produces errors.

For some reason, tellg() gives pos a value of -1 at the first assignment. Then its declined to -2, giving seekg() an invalid number and the loop breaks. It IS reading the correct file. Any idea why this happens and how to fix it?


 cfg.open(CFGFILE);
while (cfg) {
do {
c=cfg.get();
if ( cfg.eof() ) break;
} while (c < 'A' || c > 'z');
pos= cfg.tellg();
pos--;
cfg.seekg(pos);

getline(cfg, buffer1, '=');


[Edited by - Gyna on June 7, 2007 7:49:34 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this