fstream issues
-
Hi all, I just want wanted to know what the best way of loading a binary file was without using MFC (I can do it using CFile and CArchive). At the moment I want to load in a file format that composes of 7 integers into 7 different variables. I'm also a bit worried about the robustness of the procedure. At the moment I have: #include fstream ifstream fin(FilePath, ios::binary) if (fin) { fin.read(var1, sizeof(int)); fin.read(var2, sizeof(int)); etc. } fin.close(); but this seems remarkably inefficient as opposed to using the (arrow, arrow)operator as with MFC. Is this the best way to do this kind of thing? I know there is a function fin.eof() which tests for the end of the file, but surely I can't add that before each variable extraction? Its more used for text files and character extraction isn't it? I just need the best way of going about loading the data in this format into the seven different variables WITHOUT MFC. Thanks to all who are thinking about a solution. Alan. "When I left you I was but the learner, now I am the master" - Darth Vader
-
Hi all, I just want wanted to know what the best way of loading a binary file was without using MFC (I can do it using CFile and CArchive). At the moment I want to load in a file format that composes of 7 integers into 7 different variables. I'm also a bit worried about the robustness of the procedure. At the moment I have: #include fstream ifstream fin(FilePath, ios::binary) if (fin) { fin.read(var1, sizeof(int)); fin.read(var2, sizeof(int)); etc. } fin.close(); but this seems remarkably inefficient as opposed to using the (arrow, arrow)operator as with MFC. Is this the best way to do this kind of thing? I know there is a function fin.eof() which tests for the end of the file, but surely I can't add that before each variable extraction? Its more used for text files and character extraction isn't it? I just need the best way of going about loading the data in this format into the seven different variables WITHOUT MFC. Thanks to all who are thinking about a solution. Alan. "When I left you I was but the learner, now I am the master" - Darth Vader
If the numbers are stored in binary format, then your code is just fine, and probably is as efficient as any MFC method. iostreams also have operators
<<
and>>
, but these are meant to handle numbers expressed in text mode. The code can be refined a little with the use of exceptions:ifstream fin;
fin.exceptions(ifstream::eofbit|ifstream::failbit|ifstream::badbit);
try{
fin.open(FilePath, ios::binary);
fin.read(var1, sizeof(int));
fin.read(var2, sizeof(int));
...
}
catch(ifstream::failure& e){
// something bad happened
}
fin.close();Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
If the numbers are stored in binary format, then your code is just fine, and probably is as efficient as any MFC method. iostreams also have operators
<<
and>>
, but these are meant to handle numbers expressed in text mode. The code can be refined a little with the use of exceptions:ifstream fin;
fin.exceptions(ifstream::eofbit|ifstream::failbit|ifstream::badbit);
try{
fin.open(FilePath, ios::binary);
fin.read(var1, sizeof(int));
fin.read(var2, sizeof(int));
...
}
catch(ifstream::failure& e){
// something bad happened
}
fin.close();Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Thanks for the reply Joaquin, I was wondering how to use the try catch exceptions without MFC (I have used CException but obviously can't if I'm not using MFC) and you have given it to me on a plate, so many thanks for that :). I also thank you for reaffirming the loading method, you know sometimes when you get that feeling when something doesn't quite feel right (thats what I got)? I think your example looks a lot sweeter, mind, with the try catch stuff in it, its exactly what I was looking for. Again, many thanks Joaquin, may the code be with you. Alan. "When I left you I was but the learner, now I am the master" - Darth Vader