473,246 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

A Problem About fstream open mode !!

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
5 3911

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marc Schellens | last post by:
fstream.open( name, mode) fails fstream.fail() returns true. How can I find out what went wrong? (ie. file does not exist for reading, permissions...) thanks, marc
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
14
by: Mike - EMAIL IGNORED | last post by:
Using RH9, from this fragment: fstream theStream_; string fPath = "myFile"; int mode = O_EXCL | O_CREAT; theStream_.open(fPath.c_str(),mode); I get the failure shown below. ...
3
by: dumboo | last post by:
hi there i have been trying to open file using fstream, in read+write+binary mode fstream ff; ff.open(filename, ios::in | ios::out | ios::binary); now this works what i m looking for is,...
6
by: Rate Spring | last post by:
hi, in win2000 and VC6, I operate a txt file. When I write a line data into file and add "\r\n" at the end of line, I find: fstream << "\r\n" write 3 chars : 0D 0D 0A fstream << "\n" ...
6
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
5
by: Dic4000 | last post by:
下面程序建立不了文件,不知道哪里出错了? 只想定义一个fstream类型来完成输入输出的工作. #include<iostream> #include<conio.h> #include<fstream> using namespace std;
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you抣l learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.