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 a file continuously using CFile

how to read a file continuously using CFile

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
12 Posts 7 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.
  • C Cedric Moonen

    What do you mean exactly by "continuously" ? :confused: Do you mean you want to read the complete file ?

    Cédric Moonen Software developer
    Charting control [v3.0] OpenGL game tutorial in C++

    K Offline
    K Offline
    Krauze
    wrote on last edited by
    #3

    yep! In fact I wanna plot a range of x- and y-coordinates onto a graph while reading them in a txt file. So I used "continuously".

    C 1 Reply Last reply
    0
    • K Krauze

      yep! In fact I wanna plot a range of x- and y-coordinates onto a graph while reading them in a txt file. So I used "continuously".

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #4

      The Read function returns the number of bytes read. When there's nothing more to read (because you reached the end of the file), the function will return 0.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      K 1 Reply Last reply
      0
      • C Cedric Moonen

        The Read function returns the number of bytes read. When there's nothing more to read (because you reached the end of the file), the function will return 0.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        K Offline
        K Offline
        Krauze
        wrote on last edited by
        #5

        Thx a lot.

        1 Reply Last reply
        0
        • K Krauze

          Personally, it'd be like the following:

          char str\[3\];
          CFile file;
          .... // other preliminary codes before reading it
          while( !EOF() )
          {
              file.Read( &str, 3 );
          }
          file.Close();
          

          But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #6

          What's wrong with:

          while( file.Read( &str, 3 ) == 3 )
          {
          // Do stuff with the three characters read
          }

          That will terminate the loop if it can't read 3 bytes from the file. Cheers, Ash

          modified on Monday, September 6, 2010 12:17 PM

          L 1 Reply Last reply
          0
          • A Aescleal

            What's wrong with:

            while( file.Read( &str, 3 ) == 3 )
            {
            // Do stuff with the three characters read
            }

            That will terminate the loop if it can't read 3 bytes from the file. Cheers, Ash

            modified on Monday, September 6, 2010 12:17 PM

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #7

            Aescleal wrote:

            What's wrong with: while...

            The concept is OK, some details are all wrong. :laugh:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

            A 1 Reply Last reply
            0
            • L Luc Pattyn

              Aescleal wrote:

              What's wrong with: while...

              The concept is OK, some details are all wrong. :laugh:

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #8

              Don't know what you're taking about! (Thanks!) Ash

              L 1 Reply Last reply
              0
              • A Aescleal

                Don't know what you're taking about! (Thanks!) Ash

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                No problem. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                1 Reply Last reply
                0
                • K Krauze

                  Personally, it'd be like the following:

                  char str\[3\];
                  CFile file;
                  .... // other preliminary codes before reading it
                  while( !EOF() )
                  {
                      file.Read( &str, 3 );
                  }
                  file.Close();
                  

                  But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files

                  G Offline
                  G Offline
                  Gurux Ltd
                  wrote on last edited by
                  #10

                  Hi Best solution is listen events and read file after the file has changed. If you read file continously, you waste CPU time. If you want to see code sample how to do this, see: https://gurux.svn.sourceforge.net/svnroot/gurux/GXCom/GXMedias/GuruxFile GuruxFile is ATL component that listens selected file or folder and notifies when file changed. Happy coding, Mikko http://www.gurux.org

                  1 Reply Last reply
                  0
                  • K Krauze

                    Personally, it'd be like the following:

                    char str\[3\];
                    CFile file;
                    .... // other preliminary codes before reading it
                    while( !EOF() )
                    {
                        file.Read( &str, 3 );
                    }
                    file.Close();
                    

                    But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files

                    M Offline
                    M Offline
                    MatrixDud
                    wrote on last edited by
                    #11

                    char str[4]; //it is null terminated right?
                    try
                    {
                    CFile file;
                    .... // other preliminary codes before reading it
                    while( file.Read( &str, 3 ) )
                    {
                    //Do something with str
                    }

                    file.Close();
                    }
                    catch(CFileException e)
                    {
                    //Process exception
                    }

                    1 Reply Last reply
                    0
                    • K Krauze

                      Personally, it'd be like the following:

                      char str\[3\];
                      CFile file;
                      .... // other preliminary codes before reading it
                      while( !EOF() )
                      {
                          file.Read( &str, 3 );
                      }
                      file.Close();
                      

                      But IDK how to write the codes for "!EOF()". Or any other ways to read files continuously? Thx in advance! PS: .txt files

                      K Offline
                      K Offline
                      ken_m
                      wrote on last edited by
                      #12

                      you have got a couple mistakes on the line file.Read( &str, 3 ); First of all str by itself is provides a pointer, so there is no need to put the ampersand in front of it. Secondly, you are reading 3 bytes, but your buffer is only 3 bytes. You will have no room for the terminating zero character, although one may be supplied by the file itself. The code should be: file.Read( str, 2 ); str[ 2 ] = '\0'; Refer to MSDN for further information http://msdn.microsoft.com/en-us/library/ctka0kks%28v=VS.80%29.aspx[^]

                      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