Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. fstream issues

fstream issues

Scheduled Pinned Locked Moved C / C++ / MFC
c++iosquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alan Chambers
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • A Alan Chambers

      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

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        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

        A Offline
        A Offline
        Alan Chambers
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups