473,406 Members | 2,713 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,406 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 3920

"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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.