By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,837 Members | 1,681 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,837 IT Pros & Developers. It's quick & easy.

A Problem About fstream open mode !!

P: n/a
The following program is used to open a file in mode both read and
append, but it has
a problem that the file can't be opened.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
// open in both input and append mode
fstream inOut("copy.out", ios_base::in|ios_base::app);
//fstream inOut("copy.out", ios_base::in);
if (!inOut)
{
cerr << "failed to open file!" << endl;
exit(1);
}
// do something with inOut
inOut.close()
return 0;
}

When I compile and run it, the result says "failed to open file!" .
But if I change to the commend line which only contain mode
ios_base::in,then
file can be opened successfully.
Why dose this happen? I wonder whether ios_base::in can't use with
ios_base::app together? But it does work in BS's book.
Can any one give me some help?

Jul 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"ehui928" <eh*****@gmail.comskrev i meddelandet
news:11*********************@h48g2000cwc.googlegro ups.com...
The following program is used to open a file in mode both read and
append, but it has
a problem that the file can't be opened.

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
// open in both input and append mode
fstream inOut("copy.out", ios_base::in|ios_base::app);
//fstream inOut("copy.out", ios_base::in);
if (!inOut)
{
cerr << "failed to open file!" << endl;
exit(1);
}
// do something with inOut
inOut.close()
return 0;
}

When I compile and run it, the result says "failed to open file!" .
But if I change to the commend line which only contain mode
ios_base::in,then
file can be opened successfully.
Why dose this happen? I wonder whether ios_base::in can't use with
ios_base::app together?
You can't. The combination is not in the list of file open modes in
the standard. You can only use app with out.

If you think about it, what would read-append mean ?
But it does work in BS's book.
So Bjarne doesn't know C++? :-)

Or perhaps it has changed since he wrote it?
Bo Persson
Jul 13 '06 #2

P: n/a
TO Bo Persson:
thanks first!
You can't. The combination is not in the list of file open modes in
the standard. You can only use app with out.

If you think about it, what would read-append mean ?
Read-append means that I want write something at the end of a file
while reading it(for example, when a particular character has been read
then I want append something to the end of the file).

Now , I changed the line used to open the file to following
fstream inOut("copy.out", ios_base::in|ios_base::out|ios_base::app);

the program works fine , and get the correct result.
Anyway, I still have some doubt in it.

Jul 13 '06 #3

P: n/a
ehui928 wrote:
TO Bo Persson:
thanks first!
>You can't. The combination is not in the list of file open modes in
the standard. You can only use app with out.

If you think about it, what would read-append mean ?

Read-append means that I want write something at the end of a file
If you want to write to the file, you need to open it for writing.

Jul 13 '06 #4

P: n/a
Rolf Magnus wrote:
ehui928 wrote:
TO Bo Persson:
thanks first!
You can't. The combination is not in the list of file open modes in
the standard. You can only use app with out.

If you think about it, what would read-append mean ?
Read-append means that I want write something at the end of a file

If you want to write to the file, you need to open it for writing.
"ios::app implies ios::out.", as several (presumably old) pages seem to
claim.

http://techpubs.sgi.com/library/tpl/...B%2B/fstream.z

However, when giving just "fstream::app", the open fails, and when
giving
"fstream::app|fstream::out" as arg to the fstream constructor, it goes
OK
(linux, g++ 4.0.4). So, apparently all of them manuals are old.

Still, using app|out|in fails, so apparently one isn't suppose to open
a file both
for reading and appending. Granted, I never needed to do so, but hey,
maybe one day I might.

Jul 13 '06 #5

P: n/a
jo******@gmail.com wrote:
"ios::app implies ios::out.", as several (presumably old) pages seem to
claim.

http://techpubs.sgi.com/library/tpl/...B%2B/fstream.z
>
However, when giving just "fstream::app", the open fails, and when
giving "fstream::app|fstream::out" as arg to the fstream constructor, it
goes OK (linux, g++ 4.0.4). So, apparently all of them manuals are old.
I don't know if those manuals are old, but the API they describe is. This is
not the standard library class std::fstream. You can easily see that
because that would be defined in <fstreamand within namespace std, while
the class that manual page is referring to is defined in <fstream.hand
not in namespace std. This is probably a predecessor to the standard
fstream class.
Jul 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.