11-17-2008, 07:36 PM
|
#2
|
Franchise Player
Join Date: Nov 2006
Location: Supporting Urban Sprawl
|
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."
|
|
|
11-17-2008, 07:39 PM
|
#3
|
Crash and Bang Winger
|
Quote:
Originally Posted by Rathji
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?
|
|
|
11-17-2008, 07:45 PM
|
#4
|
Scoring Winger
|
Quote:
Originally Posted by flame3132
- while ( getline ( infile, buffer ) ) {
// Process the line
- }
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.
|
|
|
11-17-2008, 08:40 PM
|
#5
|
Crash and Bang Winger
|
Quote:
Originally Posted by cSpooge
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?
|
|
|
11-17-2008, 08:42 PM
|
#6
|
Franchise Player
Join Date: Jun 2004
Location: Calgary
|
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.
|
|
|
11-17-2008, 08:43 PM
|
#7
|
Scoring Winger
|
Quote:
Originally Posted by flame3132
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;
}
|
|
|
11-17-2008, 08:58 PM
|
#8
|
The new goggles also do nothing.
Join Date: Oct 2001
Location: Calgary
|
10 PRINT "Do your own homework!"
20 GOTO 10
__________________
Uncertainty is an uncomfortable position.
But certainty is an absurd one.
|
|
|
The Following 7 Users Say Thank You to photon For This Useful Post:
|
|
11-18-2008, 12:12 AM
|
#9
|
It's not easy being green!
Join Date: Oct 2001
Location: In the tubes to Vancouver Island
|
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?
|
|
|
11-18-2008, 06:35 AM
|
#10
|
Franchise Player
Join Date: Nov 2006
Location: Supporting Urban Sprawl
|
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.
|
|
|
11-18-2008, 08:49 AM
|
#11
|
Franchise Player
Join Date: Aug 2005
Location: Violating Copyrights
|
TO STREET
pu
setpos [-250 0]
pd
rt 90
repeat 25[pu fd 10 pd fd 10]
END
|
|
|
11-18-2008, 08:54 AM
|
#12
|
Franchise Player
Join Date: Nov 2006
Location: Supporting Urban Sprawl
|
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."
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -6. The time now is 04:03 AM.
|
|