Page 1 of 1

about reading multiple lines in C++

Posted: Wed Jan 16, 2008 5:04 pm
by jesun
Hi everybody.I want to know if there exists any method to read a pair of lines simultaneously with function like getline().I have tried it but it just
let me to read a line only.Let me know the method of reading i/p format like below in C++ where I have to read both first and second line inside of a while loop.

Code: Select all

adjkajdkajdkajsdkja
kdakd;akdka;kd;akd;
thanks in advance.

Posted: Wed Jan 16, 2008 5:24 pm
by Mata
you can try whit this

Code: Select all

char a[20],b[20];
scanf("%s\n%s",&a,&b);

Posted: Thu Jan 17, 2008 7:24 am
by jesun
Actually I want to read those lines in C++'s string.
like

Code: Select all

string s[2];
int n;
while(n)
{
//read consecutive two lines in s
}
Can u help me?

Posted: Thu Jan 17, 2008 2:44 pm
by maxdiver
In C++ you can read strings only with
getline (cin, s)
or
cin >> s
So there is no such function that can read two lines into one string.

Posted: Thu Jan 17, 2008 3:49 pm
by Leonid
maxdiver wrote:In C++ you can read strings only with
getline (cin, s)
or
cin >> s
So there is no such function that can read two lines into one string.
I'd rather prefer using gets to getline, since it's much more faster.

Posted: Sun Jan 20, 2008 12:19 pm
by maxdiver
Leonid
Yes, absolutely right.
gets() is dozens of times faster than getline(), but... it's not a C++-way :)
theme of "gets vs. getline" was repeated many times at this forum, but I think I won't be bad to repeat it again:
if you read/write less than 100 kb - you can use cin/getline/cout etc. without any problems.
if you need read/write much more data - always use scanf/gets/printf.
reading a megabyte using cin/getline may take about a second and even more...