Why is first line of file not read in properly?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kino
    New Member
    • Feb 2011
    • 19

    Why is first line of file not read in properly?

    I am using this function to get data from a file

    Code:
    void get()
    {
           string line;
           ifstream myfile;
           myfile.open ("jty.txt");
       
       while (getline(myfile, line))
       {  
              myfile >> one[i].a >> one[i].b >> one[i].c;
             i++;
       }
       
       
       myfile.close();
    }
    where 'one' is a struct with three int variables a, b, c.

    The file is of the form

    1 12 14
    2 1 1
    3 1 1
    4 13 1
    6 1 12
    6 1 04


    When I output this data, the output is of the form


    2 1 1
    3 1 1
    4 13 1
    6 1 12
    6 1 04


    That is the first line is not read properly, but the other lines come out fine. Could anyone please let me know the problem?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It probably has to do with your indexing. What's i before you start to copy the data into the array? Where's the code where you output the data?

    Comment

    • Oralloy
      Recognized Expert Contributor
      • Jun 2010
      • 988

      #3
      When you run your output loop, are you starting from index 1 or index 0?

      Cheers!
      Oralloy

      Comment

      • kino
        New Member
        • Feb 2011
        • 19

        #4
        Thanks for the replies!

        There seems to be nothing wrong with the indexing since I initialized i as zero before copying the data with the code above. And in my output code I am just using a for loop going from 0 to maximum. The problem occurs somewhere in the copying.

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          In the loop, print the variable line before the copy and then print the variable one after the copy and see what you get. Also print i while you're at it just to be sure.

          Comment

          • kino
            New Member
            • Feb 2011
            • 19

            #6
            Yep, I tried that, but it just copies the second row instead of the first. However, I just replaced getline(myfile, line)) with ( !myfile.eof() ) in the while loop and this gives the right output. Is there anything wrong in the way I use getline then?

            Comment

            • Oralloy
              Recognized Expert Contributor
              • Jun 2010
              • 988

              #7
              Perhaps because your loop is top driven using getline?

              That means that the first pass through, the first line of your file is consumed before the value extractions (line 9) are done.

              Cheers!
              Oralloy

              Comment

              • kino
                New Member
                • Feb 2011
                • 19

                #8
                That must be it. I tried 'do while' and now it works perfectly. Thanks a lot! :)
                And thanks once again to both of you for all the replies.

                Comment

                Working...