Page 1 of 1

read til the end of line

Posted: Mon Sep 12, 2005 7:25 am
by zero_cool
does anyone know how to read data from file until the end of line in c++?

Posted: Mon Sep 12, 2005 8:21 am
by Krzysztof Duleba

Code: Select all

while(true){
    sometype val;
    cin >> val;
    if(! cin)break;
}

Re: read til the end of line

Posted: Mon Sep 12, 2005 8:49 am
by misof
zero_cool wrote:does anyone know how to read data from file until the end of line in c++?
Actually, Krzysztof probably misread your question, his code reads data until the end of file. The most-C++ way of reading data until the end of line is probably the following one:

Code: Select all

string S;
getline(cin,S);
stringstream sin(S);
while (sin >> S) cout << "Next token: " << S << endl;

Posted: Mon Sep 12, 2005 10:21 am
by Krzysztof Duleba
Right, sorry. 9 AM is too early for me :-)

Posted: Mon Sep 12, 2005 6:54 pm
by zero_cool
thx. that was a great help.

Posted: Mon Sep 12, 2005 11:09 pm
by zero_cool
does anyone know how to read til the end of line without using string???

Posted: Sun Sep 18, 2005 4:28 pm
by Project
zero_cool wrote:does anyone know how to read til the end of line without using string???
I've seen something like that on this board. There was something with cin.peek ...

Posted: Tue Nov 08, 2005 4:00 am
by lbfacci
I do something like

Code: Select all

int a, b;
char c;

scanf("%d%c", &a, &c);
while(c != '\n' && b == 2)
	b = scanf("%d%c", &a, &c);
But you have to be sure that there is no trailing spaces (usually there arent).