dimanche 30 décembre 2018

Reading from a .txt file and looping until new line

I am having trouble understanding the output of my very little program which should read characters from a text file until it finds a new line. It correctly outputs the characters and stops, but I don't understand why it still outputs the newline ('\n) character in the terminal and doesn't end before getting to it. I know that I could use getline() or find another way but I would really understand the reason behind this behaviour. Thank you in advance! Edo

Code:

int main() {
    std::ifstream in_file;
    in_file.open("../responses.txt");
    char c;
    while(c != '\n'){
        in_file.get(c);
        std::cout << c << std::endl;
    }
    return 0;
}

Output:

A
B
C
D
E


Time elapsed: 000:00:000

The

Aucun commentaire:

Enregistrer un commentaire