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 get a line of text file

how to get a line of text file

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
10 Posts 6 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.
  • N Offline
    N Offline
    nomi khan
    wrote on last edited by
    #1

    Ive managed to read a whole text file into a string. The text file has 200 lines. I wanna read a specific line from the string, how can i do it? I tried getline(); but the problem is that it reads a line rite from the starting position of the string but not from a specific point.

    nOmI

    CPalliniC H S K 4 Replies Last reply
    0
    • N nomi khan

      Ive managed to read a whole text file into a string. The text file has 200 lines. I wanna read a specific line from the string, how can i do it? I tried getline(); but the problem is that it reads a line rite from the starting position of the string but not from a specific point.

      nOmI

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Well, you may read a line at time or may move a pointer inside the buffer. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      N H 2 Replies Last reply
      0
      • CPalliniC CPallini

        Well, you may read a line at time or may move a pointer inside the buffer. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        N Offline
        N Offline
        nomi khan
        wrote on last edited by
        #3

        kindly would u like to elaborate ur reply bcoz im a bit new in C++ programming.

        nOmI

        D 1 Reply Last reply
        0
        • N nomi khan

          kindly would u like to elaborate ur reply bcoz im a bit new in C++ programming.

          nOmI

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

          Instead of reading the whole file at once, read it one line at a time until you get to the desired line. Otherwise, you'll need to parse the buffer (a line will by separated by a \r\n pair).

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          CPalliniC 1 Reply Last reply
          0
          • N nomi khan

            Ive managed to read a whole text file into a string. The text file has 200 lines. I wanna read a specific line from the string, how can i do it? I tried getline(); but the problem is that it reads a line rite from the starting position of the string but not from a specific point.

            nOmI

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            Did you try with CFile::Read?

            1 Reply Last reply
            0
            • CPalliniC CPallini

              Well, you may read a line at time or may move a pointer inside the buffer. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #6

              CPallini wrote:

              Well, you may read a line at time or may move a pointer inside the buffer.

              And you got 1 vote at time but now you get a pointer of 5. :laugh:

              CPalliniC 1 Reply Last reply
              0
              • H Hamid Taebi

                CPallini wrote:

                Well, you may read a line at time or may move a pointer inside the buffer.

                And you got 1 vote at time but now you get a pointer of 5. :laugh:

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Thank you, pal. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • D David Crow

                  Instead of reading the whole file at once, read it one line at a time until you get to the desired line. Otherwise, you'll need to parse the buffer (a line will by separated by a \r\n pair).

                  "Love people and use things, not love things and use people." - Unknown

                  "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Thank you for elaborating my lazy answer. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • N nomi khan

                    Ive managed to read a whole text file into a string. The text file has 200 lines. I wanna read a specific line from the string, how can i do it? I tried getline(); but the problem is that it reads a line rite from the starting position of the string but not from a specific point.

                    nOmI

                    S Offline
                    S Offline
                    Sandeep Saini SRE
                    wrote on last edited by
                    #9

                    This function takes two parameters 1. first one is the path of file to be read. 2. second parametrs is the line number you want to get from the file.

                    CString GetLine(CString sFileName, int nLineNumber)
                    {
                    CStdioFile Inputfile;
                    CString sLine = _T("");
                    CFileException FileExc;
                    UINT nOpenFlags = CFile::modeRead;

                    //OPen the file & load it into a CStdioFile object
                    if (!Inputfile.Open(sFileName, nOpenFlags, &FileExc)) {
                    FileExc.ReportError();
                    return "Error";
                    }

                    //Reading line by line
                    while(Inputfile.ReadString(sLine) && nCount < nLineNumber){
                    	nCount++;
                    }
                    Inputfile.Close();
                    

                    return sLine;
                    }

                    1 Reply Last reply
                    0
                    • N nomi khan

                      Ive managed to read a whole text file into a string. The text file has 200 lines. I wanna read a specific line from the string, how can i do it? I tried getline(); but the problem is that it reads a line rite from the starting position of the string but not from a specific point.

                      nOmI

                      K Offline
                      K Offline
                      kapardhi
                      wrote on last edited by
                      #10

                      You may open the text file using "CStdioFile", the read each line using ReadString(). Actually ReadString () reads each line at a time !!

                      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