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. Simple file I/O is giving me trouble

Simple file I/O is giving me trouble

Scheduled Pinned Locked Moved C / C++ / MFC
iosperformancequestionlounge
5 Posts 4 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.
  • G Offline
    G Offline
    georgiek50
    wrote on last edited by
    #1

    I am trying to read the entire content of a text file with the following code: File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close(); The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned?

    S M D 3 Replies Last reply
    0
    • G georgiek50

      I am trying to read the entire content of a text file with the following code: File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close(); The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned?

      S Offline
      S Offline
      Sujan Christo
      wrote on last edited by
      #2

      Hi, Think having some prob with EOF state. Have a look @ msdn for basic_istream::seekg. This may help you Sujan

      1 Reply Last reply
      0
      • G georgiek50

        I am trying to read the entire content of a text file with the following code: File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close(); The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned?

        M Offline
        M Offline
        Mad__
        wrote on last edited by
        #3

        Hi For reading entire file content, try use block (not stream) i/o. FILE *pFile = fopen(fname, fmode); // fmode = "r" for text files and "rb" for binary data fseek(pFile, 0, SEEK_END); long len = ftell(pFile); char *data = new char[len]; fseek(pFile, 0, SEEK_SET); long rest = len; long tpos = 0; while(rest) { long rd = fread(data+tpos, 1, rest, pFile); if (!rd) { // check error } else { rest -= rd; tpos += rd; } fclose(pFile)

        1 Reply Last reply
        0
        • G georgiek50

          I am trying to read the entire content of a text file with the following code: File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close(); The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          How are you opening the file? If the file is opened as "text" then any EOF character encountered before the last one will cause the get() call to end prematurely.


          "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

          G 1 Reply Last reply
          0
          • D David Crow

            How are you opening the file? If the file is opened as "text" then any EOF character encountered before the last one will cause the get() call to end prematurely.


            "Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow

            G Offline
            G Offline
            georgiek50
            wrote on last edited by
            #5

            Thanks for the replies...yes the file is being opened as text...there shouldn't be a premature EOF character in there...but in general, what are my options while keeping this file open as text?

            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