| View previous topic :: View next topic |
| Author |
Message |
Chaosis13 Master Cheater
Reputation: 0
Joined: 14 Aug 2007 Posts: 372
|
Posted: Sun Jun 15, 2008 3:27 pm Post subject: [HELP][SOLUTION!] C++ File I/O |
|
|
I am a fairly good C++ programmer, but I am a little rusty becuase I took a break. I have done file I/O before, but nothing special... I have recently been trying to make a program that will read the lines of a program then stop when there is a blank line.
Any ideas? I have made the code, compiles fine, then try it but it goes on forever putting blank lines into the command promt window. I made my program see if there was a blank line by useing getline(var), then checking if it has a value, then breaking.
Help!
EDIT:
-SOLUTION-
| Code: | while(!file_io.eof()){
file_io.getline(var);
cout << var << endl;
} |
I used the eof() function, which checks for End Of File oppened. The while statment continues to record the lines until the End Of File is reached.
This is a new statement I found by accident on a website, although i did find it in one of my C++ books later...
Last edited by Chaosis13 on Sun Jun 15, 2008 3:57 pm; edited 1 time in total |
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Sun Jun 15, 2008 3:55 pm Post subject: |
|
|
Could you be a bit more specific? Why does it keep on putting blank lines?
To answer your question, use this code:
| Code: | if (var == "") {
//Do whatever if it is blank line
}
else {
//Do whatever if it is not a blank line
} |
Please be a bit more specific, as it is not too clear as to what you are trying to accomplish.
And what do you mean by "Reading the lines of a program"?
|
|
| Back to top |
|
 |
Chaosis13 Master Cheater
Reputation: 0
Joined: 14 Aug 2007 Posts: 372
|
Posted: Sun Jun 15, 2008 4:00 pm Post subject: |
|
|
Sorry if I wasn't specific, I was making a program that reads the lines of a file(program), and I had something like your code along with a while statement, but the code wouldn't break for some reason.... I still do not know what happend.
Thank you anyway. (I found solution, above).
|
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Sun Jun 15, 2008 4:03 pm Post subject: |
|
|
You could also use
| Code: | | if (myfile.good()).... |
But im glad u solved your problem!
|
|
| Back to top |
|
 |
|