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. reading from file

reading from file

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
5 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.
  • R Offline
    R Offline
    Rajveer
    wrote on last edited by
    #1

    How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?

    L A S R 4 Replies Last reply
    0
    • R Rajveer

      How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?

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

      You could use ifstream::getline(...) to remove unwanted lines.

      1 Reply Last reply
      0
      • R Rajveer

        How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?

        A Offline
        A Offline
        alex barylski
        wrote on last edited by
        #3

        I have used that approach before. Probably not the best, but it works. Only you should take care when compiling under linux/unix...I don't think they support the same CRLF sequence I think it's just CR on those systems. "An expert is someone who has made all the mistakes in his or her field" - Niels Bohr

        1 Reply Last reply
        0
        • R Rajveer

          How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?

          S Offline
          S Offline
          Shog9 0
          wrote on last edited by
          #4

          As mentioned previously, getline() will prolly work well for you. But if you want to do it your way, you should test also for EOF, just in case there are less than two lines in the file. ;)_**

          Sometimes i only remember, The days when i was young
          Nowadays no one remembers when they were young and stupid...

          **_

          ADEMA, The Way You Like It

          1 Reply Last reply
          0
          • R Rajveer

            How do I read from a file starting at a particular line. Suppose I want to start retrieving data from the third line but I don't know how much data is on the first two lines. Can I just read until I get two carriage returns? Something like: ifstream file(test.in); char test; for(int i=0; i<2; i++) { while(test!='\n') file >> test; } I know this code is most probably not right, I just put it in to help explain what it is I want I'm trying to accomplish. Can anyone tell me the right way to do it?

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #5

            If you don't mind using vanilla C, you can use fgets() to read one line at a time. /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

            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