Calgarypuck Forums - The Unofficial Calgary Flames Fan Community

Go Back   Calgarypuck Forums - The Unofficial Calgary Flames Fan Community > Main Forums > The Off Topic Forum > Tech Talk
Register Forum Rules FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread
Old 11-17-2008, 07:28 PM   #1
flame3132
Crash and Bang Winger
 
Join Date: Feb 2008
Exp:
Default Any one here good at C++

  1. while ( getline ( infile, buffer ) ) {
    // Process the line
  2. }

I'm reading a file from the stream to display in the console, but it only displays the first line how do I display the other lines?
flame3132 is offline   Reply With Quote
Old 11-17-2008, 07:36 PM   #2
Rathji
Franchise Player
 
Rathji's Avatar
 
Join Date: Nov 2006
Location: Supporting Urban Sprawl
Exp:
Default

C isn't my strong point, and specific C functions are even less so.

If getline returns an int > 1 whenver it successfully executes, then it should work.

If it doesn't then that is why it isn't running past the first loop.

Maybe something about going to next line?, skipping newline and carriage returns? just a random guess
__________________
"Wake up, Luigi! The only time plumbers sleep on the job is when we're working by the hour."
Rathji is offline   Reply With Quote
Old 11-17-2008, 07:39 PM   #3
flame3132
Crash and Bang Winger
 
Join Date: Feb 2008
Exp:
Default

Quote:
Originally Posted by Rathji View Post
C isn't my strong point, and specific C functions are even less so.

If getline returns an int > 1 whenver it successfully executes, then it should work.

If it doesn't then that is why it isn't running past the first loop.

Maybe something about going to next line?, skipping newline and carriage returns? just a random guess
Well thanks for trying the problem is that it can only display the first line how do I display ALL the lines?
flame3132 is offline   Reply With Quote
Old 11-17-2008, 07:45 PM   #4
cSpooge
Scoring Winger
 
cSpooge's Avatar
 
Join Date: Jan 2006
Exp:
Default

Quote:
Originally Posted by flame3132 View Post
  1. while ( getline ( infile, buffer ) ) {
    // Process the line
  2. }

I'm reading a file from the stream to display in the console, but it only displays the first line how do I display the other lines?

you might want to change it to while (!infile.eof()) the call get line within the loop rather than doing it in a conditional.
cSpooge is offline   Reply With Quote
Old 11-17-2008, 08:40 PM   #5
flame3132
Crash and Bang Winger
 
Join Date: Feb 2008
Exp:
Default

Quote:
Originally Posted by cSpooge View Post
you might want to change it to while (!infile.eof()) the call get line within the loop rather than doing it in a conditional.

Code:
 
int main()
{

string name;

cout << "Please enter the filename of the encoded message:";

getline(cin,name);


ifstream infile;
infile.open(name.c_str());

if (infile.fail()) {
cout << name << " fail to open." << endl;
exit(1);
}


cout << name << endl;

cout << " File successfully opened" << endl;

string buffer;
getline(infile, buffer);

while ( infile.eof() ) {

cout << buffer << endl;

}

return 0;
}
This only displays 1 line how do I get the other lines?
flame3132 is offline   Reply With Quote
Old 11-17-2008, 08:42 PM   #6
Dan02
Franchise Player
 
Dan02's Avatar
 
Join Date: Jun 2004
Location: Calgary
Exp:
Default

probably because in your while not eof loop you're never changing the value in buffer,

should it not be while( !inline.eof()) as well?

how about something likke

Quote:
int main()
{

string name;

cout << "Please enter the filename of the encoded message:";

getline(cin,name);


ifstream infile;
infile.open(name.c_str());

if (infile.fail()) {
cout << name << " fail to open." << endl;
exit(1);
}


cout << name << endl;

cout << " File successfully opened" << endl;

string buffer;
getline(infile, buffer);

while ( !infile.eof() ) {

cout << buffer << endl;
getline(infile, buffer);

}

return 0;
}
Been awhile since i did any code though so maybe it sucks

Last edited by Dan02; 11-17-2008 at 08:45 PM.
Dan02 is offline   Reply With Quote
Old 11-17-2008, 08:43 PM   #7
cSpooge
Scoring Winger
 
cSpooge's Avatar
 
Join Date: Jan 2006
Exp:
Default

Quote:
Originally Posted by flame3132 View Post
Code:
 
int main()
{

string name;

cout << "Please enter the filename of the encoded message:";

getline(cin,name);


ifstream infile;
infile.open(name.c_str());

if (infile.fail()) {
cout << name << " fail to open." << endl;
exit(1);
}


cout << name << endl;

cout << " File successfully opened" << endl;

string buffer;
getline(infile, buffer);

while ( infile.eof() ) {

cout << buffer << endl;

}

return 0;
}
This only displays 1 line how do I get the other lines?
should be

Code:
 
while( !infile.eof())
{
getline(infile, buffer);
cout << buffer << endl;
}
cSpooge is offline   Reply With Quote
Old 11-17-2008, 08:58 PM   #8
photon
The new goggles also do nothing.
 
photon's Avatar
 
Join Date: Oct 2001
Location: Calgary
Exp:
Default

10 PRINT "Do your own homework!"
20 GOTO 10
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
photon is offline   Reply With Quote
The Following 7 Users Say Thank You to photon For This Useful Post:
Old 11-18-2008, 12:12 AM   #9
kermitology
It's not easy being green!
 
kermitology's Avatar
 
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
Exp:
Default

Yeah, do your own work!

I still would have suggested the use of finding the EOF. You'd crash if you didn't anyways.
__________________
Who is in charge of this product and why haven't they been fired yet?
kermitology is offline   Reply With Quote
Old 11-18-2008, 06:35 AM   #10
Rathji
Franchise Player
 
Rathji's Avatar
 
Join Date: Nov 2006
Location: Supporting Urban Sprawl
Exp:
Default

Yeah, EOF is the way I have always done it. I just assumed getline() was some library use of while(!EOF) but I have zero knowledge of anything that is not the most basic c standard library.
__________________
"Wake up, Luigi! The only time plumbers sleep on the job is when we're working by the hour."

Last edited by Rathji; 11-18-2008 at 06:41 AM.
Rathji is offline   Reply With Quote
Old 11-18-2008, 08:49 AM   #11
Barnes
Franchise Player
 
Barnes's Avatar
 
Join Date: Aug 2005
Location: Violating Copyrights
Exp:
Default

TO STREET
pu
setpos [-250 0]
pd
rt 90
repeat 25[pu fd 10 pd fd 10]
END
Barnes is offline   Reply With Quote
Old 11-18-2008, 08:54 AM   #12
Rathji
Franchise Player
 
Rathji's Avatar
 
Join Date: Nov 2006
Location: Supporting Urban Sprawl
Exp:
Default

Or try LOL Code

Code:
Hai
can has stdio?
Plz open file "lolcats.txt"?
	Awsum thx
		visible file
	o noes
		invisible "error!"
kthxbye
__________________
"Wake up, Luigi! The only time plumbers sleep on the job is when we're working by the hour."
Rathji is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 05:48 AM.

Calgary Flames
2024-25




Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright Calgarypuck 2021 | See Our Privacy Policy