-1

Hi I am new here and i need help for my assignment. I want to read data from a file. I've used getline but it wont read the first line inside the file, can someone help me please? Here is my code:

void test(std::ifstream& infile, string& a, string& b, string& c) 
{
   infile >> a;
   infile >> b;
   infile >> c; 
}

int main() 
{
   ifstream file_("Level1.txt");
   string line;
   string a, b, c;

   while (getline(file_, line))
   {
       test(file_, a, b, c);
   }

   cout << a << " " << b << " " << c;
   return 0;
}

in my file:

aa
bb
cc

but output:

bb cc
Daaenerys
  • 11
  • 1
  • 5
  • 7
    You don't see the first line in the output, because you don't print it. The `getline` function read it, and then in the `test` function you read the input from the other two lines. Perhaps you want `while (file_ >> a >> b >> c) {cout << a << ' ' << b << ' ' << c << '\n'; }`? – Some programmer dude May 30 '17 at 15:52
  • Copy/paste programming at its best! – Lightness Races in Orbit May 30 '17 at 17:14

2 Answers2

3

Getline is getting the current line, so your strings look like this

a = "bb"
b = "cc"
c = ""

since you have already read the fist line and it is stored in line .You can just check if the file is open and then call your test function since it will start at the beginning of the file without skipping the first line.

if(file_.is_open())
{
    test(file_, a, b, c);
}
Omar Martinez
  • 666
  • 6
  • 18
2

If the stream supports peek interface (which it generally does support), you might want to check your loop condition to be like this:

while (file_.peek() != EOF)

This will do the trick (and will do exactly as I thought eof() would behave).

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
iehrlich
  • 3,524
  • 4
  • 30
  • 42
  • 5
    `while (!file_.eof())` [No, no, no, no.](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – NathanOliver May 30 '17 at 15:57
  • 5
    You might want to read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude May 30 '17 at 15:57
  • Yyyyeeeeah. This answer must be changed for sure. What about `while (file_.peek() != EOF)`? Should be working in most of the cases. – iehrlich May 30 '17 at 16:01
  • @iehrlich In this case Omar Martinez answer looks more appropriate. – NathanOliver May 30 '17 at 16:02
  • Maybe changing `test` to return the stream and doing `while(test(file_, a, b, c)){}` depending if the OP needs to keep reading values. – NathanOliver May 30 '17 at 16:04
  • @NathanOliver not really, because it changes the desired semantics of the code by reducing the number of iterations in the initial loop to 1. – iehrlich May 30 '17 at 16:04
  • Alright anyway I changed the answer to be less ignorant :) – iehrlich May 30 '17 at 16:13
  • yea! i didnt noticed the getline stored the first string into line and while (file_.peek() != EOF) works~ thanks guys! – Daaenerys May 30 '17 at 16:44
  • 1
    It's still a poor answer - you're only checking that _one_ more character can be read, but extraction into three entire tokens are then attempted. – Lightness Races in Orbit May 30 '17 at 17:14
  • @BoundaryImposition Considering the OP's sample resembles a real application, it is written in a way it expects input data in triplets. In a protected environment, we might expect that, if the message is received, it complies to the protocol - the transport layer must ensure that. That's why I assume `peek()` to be sufficient. – iehrlich May 30 '17 at 17:34
  • I see your point but think that's a bad idea, particularly in beginner code. If for no other reason than no transport layer protocol commonly used today will protect you against a truncated data stream. Just ensure that you have a full record before you attempt to do things with it; this is really easy, as shown in a comment under the question. – Lightness Races in Orbit May 30 '17 at 17:36