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. How to read from a text file?

How to read from a text file?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++helpquestion
4 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.
  • A Offline
    A Offline
    Amarelia
    wrote on last edited by
    #1

    Hi Friends, I have developed a dialog based MFC programm. I want to read a text file which contains some encrypted characters....File may be long or may be short. My problem is I don't know how to read that whole file as right now I use fread() functions to read file in which it terminates when it gets some character even if there are still more data in file. Also I tried to read it using a character by character reading by fgetc(). Still I encounter the same problem. Is there a way to read whole file till the end of file. And all characters are read. Any function or any example ? Thankx in advance. Mahesh

    C T D 3 Replies Last reply
    0
    • A Amarelia

      Hi Friends, I have developed a dialog based MFC programm. I want to read a text file which contains some encrypted characters....File may be long or may be short. My problem is I don't know how to read that whole file as right now I use fread() functions to read file in which it terminates when it gets some character even if there are still more data in file. Also I tried to read it using a character by character reading by fgetc(). Still I encounter the same problem. Is there a way to read whole file till the end of file. And all characters are read. Any function or any example ? Thankx in advance. Mahesh

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      Amarelia wrote:

      I use fread() functions to read file in which it terminates when it gets some character even if there are still more data in file

      fread() reads a specified number of bytes, and doesn't look at the characters at all: // read 100 bytes FILE *fp = fopen(filename, "rb"); BYTE buf[100]; int read = fread(buf, 1, 100, fp); fclose(fp); if you want to read the whole file, just keep calling fread(..) until it returns 0 (no bytes read). Cleek | Image Toolkits | Thumbnail maker

      1 Reply Last reply
      0
      • A Amarelia

        Hi Friends, I have developed a dialog based MFC programm. I want to read a text file which contains some encrypted characters....File may be long or may be short. My problem is I don't know how to read that whole file as right now I use fread() functions to read file in which it terminates when it gets some character even if there are still more data in file. Also I tried to read it using a character by character reading by fgetc(). Still I encounter the same problem. Is there a way to read whole file till the end of file. And all characters are read. Any function or any example ? Thankx in advance. Mahesh

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        i think you problem may come from you write in a buffer not large enough. do this : 1. get the number of characters in the file to be exctracted. 2. allocate your buffer with that number of bytes. 3. read() the totality of the file ; it will never overflow the buffer (considering the memory is correctly allocated)


        TOXCCT >>> GEII power
        [toxcct][VisualCalc 2.20][VisualCalc 3.0]

        1 Reply Last reply
        0
        • A Amarelia

          Hi Friends, I have developed a dialog based MFC programm. I want to read a text file which contains some encrypted characters....File may be long or may be short. My problem is I don't know how to read that whole file as right now I use fread() functions to read file in which it terminates when it gets some character even if there are still more data in file. Also I tried to read it using a character by character reading by fgetc(). Still I encounter the same problem. Is there a way to read whole file till the end of file. And all characters are read. Any function or any example ? Thankx in advance. Mahesh

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

          Amarelia wrote:

          ...it terminates when it gets some character...

          Is it encountering an EOF character? If so, you need to open the file as binary, not text.


          "The words of God are not like the oak leaf which dies and falls to the earth, but like the pine tree which stays green forever." - Native American Proverb

          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