|
-
February 17th, 2004, 07:47 PM
#1
fstream issues with ios::app
Having an issue with ios::app. I'm using the Xcode application on my Mac to compile it. Here's the code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buffer[250];
fstream myfile;
myfile.open("testtext", ios::out | ios::trunc);
if(myfile.is_open())
{
myfile << "This is output \n";
myfile.close();
}
myfile.open("testtext", ios::in);
myfile.getline(buffer, 100);
cout << "The file contains " << buffer << endl;
myfile.close();
myfile.open("testtext", ios::app);
myfile << "Hey this is YET another line of output \n";
myfile.close();
myfile.open("testtext", ios::in);
myfile.getline(buffer, 100);
cout << "The file contains " << buffer << endl;
myfile.close();
return 0;
}
When the compiled program runs:
The file contains This is output
The file contains
It leaves a blank after the second output. Why? I'm a beginner at learning C++ but I'm pretty sure that the second output should display both things written to the file right? Because "app" was supposed to "append" the line to the file therefore making two lines? So, yeah, I'm confused. Really would like some help on this please.
-
February 17th, 2004, 08:53 PM
#2
Don't explicitly close your fstreams, let them fall out of scope. Also, I think you have to do a fstream.clear() in order to open (or re-open) a file.
Why are you using an old C-Style string and why are you using a member getline() function?
-
February 17th, 2004, 08:59 PM
#3
Originally posted by kasracer
Don't explicitly close your fstreams, let them fall out of scope. Also, I think you have to do a fstream.clear() in order to open (or re-open) a file.
Why are you using an old C-Style string and why are you using a member getline() function?
Just following what my c++ book says to do.
-
February 17th, 2004, 09:05 PM
#4
Originally posted by modyouup
Just following what my c++ book says to do.
Burn it
-
February 17th, 2004, 09:19 PM
#5
When you use an "ifstream()" , ios::in is automatically added,
even if you do not specify it. If you open an "ofstream()",
ios::out is automatically added as the mode. If you use
"fsteam()", there is nothing automatically added. You are
using fstream, so the only modes present are what you
explicitly specify.
When you open to append :
Code:
myfile.open("testtext", ios::app);
The only mode is "ios::app". This is illegal. If you use ios::app,
you must have ios::out. For an ofstream this is automatic, but
not for an fstream. So you need to add it :
Code:
myfile.open("testtext", ios::out | ios::app);
-
February 17th, 2004, 09:41 PM
#6
I also want to add in to make a question,
Philip, could Philip tell me when I have to use ios::bin, I have a book but it doesnt tell me anything but an example to ask me to figure it out myself, please help...
Thanks Philip
-
February 17th, 2004, 10:02 PM
#7
1) to the original question ... also, the second time you
open to read, you only read 1 line, even though there
will be 2 lines in the file. You can loop to get all the lines:
Code:
while (myfile.getline(buffer, 100))
{
cout << "The file contains " << buffer << endl;
}
myfile.close();
return 0;
2) as for when to use ios::bin it's hard to have a hard-and-fast
rule. When obvious time to use binary is when reading
files such as BMP , AVI , JPG files.
-
February 17th, 2004, 10:17 PM
#8
Originally posted by Philip Nicoletti
2) as for when to use ios::bin it's hard to have a hard-and-fast
rule. When obvious time to use binary is when reading
files such as BMP , AVI , JPG files.
Thanks Philip a lot,
-
February 18th, 2004, 01:31 PM
#9
Thanks! Still a little problem though. This is my current code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buffer[250];
fstream myfile;
myfile.open("testtext", ios::out | ios::trunc);
if(myfile.is_open())
{
myfile << "This is output \n";
myfile.close();
}
myfile.open("testtext", ios::in);
myfile.getline(buffer, 100);
cout << "The file contains " << buffer << endl;
myfile.close();
myfile.open("testtext", ios::out | ios::app);
myfile << "Hey this is YET another line of output \n";
myfile.close();
myfile.open("testtext", ios::in);
myfile.getline(buffer, 100);
while (myfile.getline(buffer, 100))
{
cout << "The file contains " << buffer << endl;
}
myfile.close();
return 0;
}
When I run the program:
The file contains This is output
The file contains Hey this is YET another line of output
Shouldn't the second output show both lines? Yet it shows only the second line. odd
Last edited by modyouup; February 18th, 2004 at 01:34 PM.
-
February 18th, 2004, 02:01 PM
#10
You do an initial getline() before the while loop, so that
line never gets printed. Remove that line.
Code:
myfile.getline(buffer, 100); // remove this line
while (myfile.getline(buffer, 100))
{
cout << "The file contains " << buffer << endl;
-
February 18th, 2004, 02:06 PM
#11
Originally posted by Philip Nicoletti
You do an initial getline() before the while loop, so that
line never gets printed. Remove that line.
Code:
myfile.getline(buffer, 100); // remove this line
while (myfile.getline(buffer, 100))
{
cout << "The file contains " << buffer << endl;
Removed line, now it gives this when I run...is this correct?:
The file contains This is output
The file contains This is output
The file contains Hey this is YET another line of output
Didn't expect it to say "the file contains" three times.
-
February 18th, 2004, 02:20 PM
#12
Yes that is correct. getline() reads one line in the file (where
the line delimiter is '\n') - it does not read the entire file.
You can sometimes "trick" getline into reading an entire
text file at once by putting the delimiter character (the
third argument which is '\n' by default) to some character
not found in the text file. NULL is a good example, since
most "normal" text files will not have it. I modified the
second read section below (and put a new line after
the "The file contains" for readability :
Code:
myfile.open("testtext", ios::in);
while (myfile.getline(buffer, 100, 0))
{
cout << "The file contains :\n" << buffer << endl;
}
myfile.close();
But the "normal way" to do it would be :
Code:
myfile.open("testtext", ios::in);
cout << "The file contains :\n";
while (myfile.getline(buffer, 100))
{
cout << buffer << endl;
}
myfile.close();
or even :
Code:
myfile.open("testtext", ios::in);
cout << "The file contains :\n" << myfile.rdbuf() << endl;
myfile.close();
-
February 18th, 2004, 03:48 PM
#13
OK, so in it's normal operation, getline will always just read one line. So if there's more than one read, it'll always repeat itself and put the rest on the next line. Mmmm, ok got it. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|