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. Spurious end of file error from ifstream::read

Spurious end of file error from ifstream::read

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++cssioscom
2 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
    Alexander Kindel
    wrote on last edited by
    #1

    I'm using ofstream and ifstream for serialization purposes, using this function to read values from the file back into variables:

    template
    T read(std::ifstream&savedGame)
    {
    char buffer[sizeof(T)];
    savedGame.read(buffer, sizeof(T));
    return *(T*)&buffer;
    }

    This is the file I'm reading out of: test.chess - Google Drive[^] The deserialization works until the point that I try to do a 4-byte read whose first value corresponds to the N with the tilde a little less than halfway through that file, as viewed in notepad. At that point, the watch window tells me the first three entries of buffer get filled, matching the file, with a tilda N, an ellipsis, and a trademark symbol (hexidecimal values D1, 85, and 99), while the fourth entry is left uninitialized. When I call savedGame.rdstate(), I get a value of 3, which means eofbit and failbit are set: ios_base::iostate - C++ Reference[^]. All subsequent reads leave the whole buffer uninitialized. I can see that the file goes on after that - the character that should have gone in the fourth buffer entry has hexidecimal value 1A - so what could the problem be? I can post more of the surrounding code if that would be relevant.

    L 1 Reply Last reply
    0
    • A Alexander Kindel

      I'm using ofstream and ifstream for serialization purposes, using this function to read values from the file back into variables:

      template
      T read(std::ifstream&savedGame)
      {
      char buffer[sizeof(T)];
      savedGame.read(buffer, sizeof(T));
      return *(T*)&buffer;
      }

      This is the file I'm reading out of: test.chess - Google Drive[^] The deserialization works until the point that I try to do a 4-byte read whose first value corresponds to the N with the tilde a little less than halfway through that file, as viewed in notepad. At that point, the watch window tells me the first three entries of buffer get filled, matching the file, with a tilda N, an ellipsis, and a trademark symbol (hexidecimal values D1, 85, and 99), while the fourth entry is left uninitialized. When I call savedGame.rdstate(), I get a value of 3, which means eofbit and failbit are set: ios_base::iostate - C++ Reference[^]. All subsequent reads leave the whole buffer uninitialized. I can see that the file goes on after that - the character that should have gone in the fourth buffer entry has hexidecimal value 1A - so what could the problem be? I can post more of the surrounding code if that would be relevant.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hex 1A (equivalent to Ctrl-Z) is an end of file byte in a text file. You need to change your code so that you open the file in binary mode.

      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