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 line-by-line?

How to read a file line-by-line?

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorialquestion
6 Posts 5 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.
  • I Offline
    I Offline
    IGx89
    wrote on last edited by
    #1

    Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!

    Z M W V 4 Replies Last reply
    0
    • I IGx89

      Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!

      Z Offline
      Z Offline
      ZoogieZork
      wrote on last edited by
      #2

      Take a look at ifstream::getline and string::getline. - Mike

      1 Reply Last reply
      0
      • I IGx89

        Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        fgets

        I 1 Reply Last reply
        0
        • I IGx89

          Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!

          W Offline
          W Offline
          W Hammer sledge
          wrote on last edited by
          #4

          You can use getline()

          #include

          void main()
          {
          char buffer[256];
          ifstream file;
          file.open("c:\\autoexec.bat", ios::in | ios::nocreate);

          if (file.good())
          {
          while(file.peek() != EOF)
          {
          file.getline(buffer,255);
          cout << buffer << endl;
          }
          file.close();
          }
          else
          cout << "error opening autoexec.bat" << endl;

          }

          the default delimiter for getline is \n, see msdn for more details. Hope this helps sledge

          1 Reply Last reply
          0
          • M Mike Dimmick

            fgets

            I Offline
            I Offline
            IGx89
            wrote on last edited by
            #5

            Thanks!

            1 Reply Last reply
            0
            • I IGx89

              Is there an easy way to read a file line-by-line, not using MFC? I looked at fread, ReadFile, and ofstream, but I couldn't find a "ReadLine" function in any of them. Thanks!

              V Offline
              V Offline
              valikac
              wrote on last edited by
              #6

              One solution is std::getline(). string sInput; std::getline(file, sInput); Kuphryn

              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