why ">>" operator in "istream" passes new lines?

HI!
Suppose this is student.txt

joe
6849
computer
diploma

bill
1859
electronic
BSc

john
78946
computer
BSc


As you see there are some informations about some people in university.

What I want to do is to read these information from file with this code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
#include <string>

using std :: cout ;
using std :: ifstream ;
using std :: string ;
using std :: endl ;

int main ()
{
    ifstream myfile ;
    string line ;
    myfile.open ("student.txt") ;
    while (! myfile.eof ())
    {
        myfile >> line ;
        cout << "first name : " << line << endl ;
        myfile >> line ;
        cout << "last name : " << line << endl ;
        myfile >> line ;
        cout << "ID : " << line << endl ;
        myfile >> line ;
        cout << "last degree : " << line << endl ;
        myfile >> line ;
        cout << "major : " << line << endl ;
        myfile >> line ;
        cout << "date registered : " << line << endl ;
    }
    return 0 ;
}


What I expected at the first was that for the second and third person the program Ignores their names , because there is a new line before them and the program will count it as the "line" for the name .
Why is that?
What if I wanted my program to count blank lines in a file?
I even made the blank lines more (for example 10 blank lines between every field of information) and still I had problem.
THank you!
In this case use function

std::getline( myfile, line );
The way >> works is that it ignores all whitespace characters up until the first non-whitespace character and then it tries to read whatever it is you are reading. The newline character is a whitespace character so it will be treated the same way as a normal space or any other whitespace character.
Topic archived. No new replies allowed.