A Problem About fstream open mode !!

E

ehui928

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?
 
B

Bo Persson

ehui928 said:
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
 
E

ehui928

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::eek:ut|ios_base::app);

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

Rolf Magnus

ehui928 said:
TO Bo Persson:
thanks first!


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.
 
J

joosteto

Rolf said:
If you want to write to the file, you need to open it for writing.

"ios::app implies ios::eek:ut.", as several (presumably old) pages seem to
claim.

http://techpubs.sgi.com/library/tpl...me=/usr/share/catman/p_man/cat3/c++/fstream.z

However, when giving just "fstream::app", the open fails, and when
giving
"fstream::app|fstream::eek:ut" 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.
 
R

Rolf Magnus

"ios::app implies ios::eek:ut.", as several (presumably old) pages seem to
claim.

http://techpubs.sgi.com/library/tpl...me=/usr/share/catman/p_man/cat3/c++/fstream.z

However, when giving just "fstream::app", the open fails, and when
giving "fstream::app|fstream::eek:ut" 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 <fstream> and within namespace std, while
the class that manual page is referring to is defined in <fstream.h> and
not in namespace std. This is probably a predecessor to the standard
fstream class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top