about reading multiple lines in C++

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
jesun
New poster
Posts: 10
Joined: Tue Jan 01, 2008 10:55 pm

about reading multiple lines in C++

Post by jesun » Wed Jan 16, 2008 5:04 pm

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.

User avatar
Mata
New poster
Posts: 18
Joined: Mon Dec 17, 2007 11:35 pm
Location: Queretaro
Contact:

Post by Mata » Wed Jan 16, 2008 5:24 pm

you can try whit this

Code: Select all

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

jesun
New poster
Posts: 10
Joined: Tue Jan 01, 2008 10:55 pm

Post by jesun » Thu Jan 17, 2008 7:24 am

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?

maxdiver
Learning poster
Posts: 51
Joined: Tue Sep 04, 2007 2:12 pm
Location: Russia, Saratov
Contact:

Post by maxdiver » Thu Jan 17, 2008 2:44 pm

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.

Leonid
Experienced poster
Posts: 146
Joined: Thu Dec 22, 2005 5:50 pm
Contact:

Post by Leonid » Thu Jan 17, 2008 3:49 pm

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.

maxdiver
Learning poster
Posts: 51
Joined: Tue Sep 04, 2007 2:12 pm
Location: Russia, Saratov
Contact:

Post by maxdiver » Sun Jan 20, 2008 12:19 pm

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...

Post Reply

Return to “C++”