473,396 Members | 1,725 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,396 software developers and data experts.

Can not open file with "ios_base::in | ios_base::app"

// Pls note that test.txt exists.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream fs("test.txt", ios_base::in | ios_base::app);
if(!fs)
{
cout << "Error while opening file!" << endl;
return 1;
}
string s;
getline(fs, s);
cout << s << endl;
fs << "hello" << endl;
return 0;
}

The output is always "Error while opening file!" even when test.txt
exists. What's the problem?

System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2

Thanks very much for your attention.

Jan 5 '07 #1
9 4966
On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:
// Pls note that test.txt exists.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream fs("test.txt", ios_base::in | ios_base::app);
The ios_base::app means "append" or "Seek to end before each write", and
only makes sense if you are opening a file for *output* (don't think it
works if ios::in is specified). Try instead just:

fstream fs("test.txt");

as the default open mode for an fstream mode is ios::in|ios::out, which
looks like what you want.
if(!fs)
{
cout << "Error while opening file!" << endl;
return 1;
}
string s;
getline(fs, s);
You should test whether that read worked...
cout << s << endl;
Looks like you want to append to the file. This sets the write position to
the end of file:

fs.seekp(0,ios::end);

fs << "hello" << endl;
Again you should test whether that write worked... and you should close
the fs once you're done.

fs.close();
return 0;
}

The output is always "Error while opening file!" even when test.txt
exists. What's the problem?

System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2

Thanks very much for your attention.
--
Lionel B
Jan 5 '07 #2
"Lionel B д
"
On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:

--
Lionel B
Thanks very much for reply.

That kind of usage (ios_base::in | ios_base::app) was found in some
books, for example, Lippman's C++ Primer 3rd Edition. Seems not
supported by newer compilers.

Jan 5 '07 #3
On Fri, 05 Jan 2007 05:44:35 -0800, Robin wrote:
"Lionel B 写道:
"
>On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:

--
Lionel B

Thanks very much for reply.

That kind of usage (ios_base::in | ios_base::app) was found in some
books, for example, Lippman's C++ Primer 3rd Edition. Seems not
supported by newer compilers.
Yes, I don't know what the Standard says (maybe someone can enlighten us
on this), but it doesn't work for me here on a recent gcc. In principle
I'm not sure why it *shouldn't* work, since read and write positions are
separately maintained.

--
Lionel B
Jan 5 '07 #4
Robin wrote:
// Pls note that test.txt exists.
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream fs("test.txt", ios_base::in | ios_base::app);
if(!fs)
{
cout << "Error while opening file!" << endl;
return 1;
}
string s;
getline(fs, s);
cout << s << endl;
fs << "hello" << endl;
return 0;
}

The output is always "Error while opening file!" even when test.txt
exists. What's the problem?

System: Linux 2.6.20-2-generic, Complier: GCC 4.1.2

Thanks very much for your attention.
A wild guess -- perhaps the file exists, but is
not accessible for you to open (sharing violation,
access rights, etc.) at the time this code is ran.

HTH,
- J.
Jan 5 '07 #5
Lionel B wrote:
On Fri, 05 Jan 2007 05:44:35 -0800, Robin wrote:
>"Lionel B ??:
"
>>On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:

--
Lionel B

Thanks very much for reply.

That kind of usage (ios_base::in | ios_base::app) was found in some
books, for example, Lippman's C++ Primer 3rd Edition. Seems not
supported by newer compilers.

Yes, I don't know what the Standard says (maybe someone can
enlighten us on this), but it doesn't work for me here on a recent
gcc. In principle I'm not sure why it *shouldn't* work, since read
and write positions are separately maintained.
Read and write positions are not separately maintained, there is just one
"stream position". You have to do a seek to change between read and write
modes.

The standard contains an explicit list of possible combinations of open
modes. The in | app combination just isn't in that list.
Bo Persson
Jan 5 '07 #6
On Fri, 05 Jan 2007 20:19:02 +0100, Bo Persson wrote:
Lionel B wrote:
>On Fri, 05 Jan 2007 05:44:35 -0800, Robin wrote:
>>"Lionel B ??:
"
On Fri, 05 Jan 2007 04:20:08 -0800, Robin wrote:

--
Lionel B

Thanks very much for reply.

That kind of usage (ios_base::in | ios_base::app) was found in some
books, for example, Lippman's C++ Primer 3rd Edition. Seems not
supported by newer compilers.

Yes, I don't know what the Standard says (maybe someone can
enlighten us on this), but it doesn't work for me here on a recent
gcc. In principle I'm not sure why it *shouldn't* work, since read
and write positions are separately maintained.

Read and write positions are not separately maintained, there is just one
"stream position". You have to do a seek to change between read and write
modes.
Is that right? I stand corrected then (I thought seekg() and seekp()
maintained separate read/write positions simultaneously).
The standard contains an explicit list of possible combinations of open
modes. The in | app combination just isn't in that list.
Right. Thanks,

--
Lionel B
Jan 5 '07 #7

Lionel B wrote in message ...
>On Fri, 05 Jan 2007 20:19:02 +0100, Bo Persson wrote:
>>Yes, I don't know what the Standard says (maybe someone can
enlighten us on this), but it doesn't work for me here on a recent
gcc. In principle I'm not sure why it *shouldn't* work, since read
and write positions are separately maintained.

Read and write positions are not separately maintained, there is just one
"stream position". You have to do a seek to change between read and write
modes.

Is that right? I stand corrected then (I thought seekg() and seekp()
maintained separate read/write positions simultaneously).
Lionel B
Bo's statement now has me confused[1]. Is it just a GCC thing?
Try this and see what output you get:

#include <iostream>
#include <sstream// or <fstream>
#include <string>

int main(){
using std::cout; // for NG post
std::stringstream ss( "Hello World!\n" );
cout<<" ss 1 tellp="<<ss.tellp()<<std::endl;
cout<<" ss 1 tellg="<<ss.tellg()<<std::endl;
ss.seekp( 0, std::ios_base::end );
cout<<" ss 2 tellp="<<ss.tellp()<<std::endl;
cout<<" ss 2 tellg="<<ss.tellg()<<std::endl;
ss.seekg( 1 );
ss << "Bite me!\n";
cout<<" ss 3 tellp="<<ss.tellp()<<std::endl;
cout<<" ss 3 tellg="<<ss.tellg()<<std::endl;
std::string Line;
std::getline( ss, Line );
cout<<" stringstream getline(ss, Line) ="<<Line<<std::endl;
cout<<" stringstream ss ="<<ss.str()<<std::endl;
}

/* - output - [ GCC MinGW 3.3.1 ]
ss 1 tellp=0
ss 1 tellg=0
ss 2 tellp=13
ss 2 tellg=0
ss 3 tellp=22
ss 3 tellg=1
stringstream getline(ss, Line) =ello World!
stringstream ss =Hello World!
Bite me!
*/

[1] - unless Bo was only refering to the 'std::ios_base::[ in|app ]' case.
--
Bob R
POVrookie
Jan 6 '07 #8
"BobR" <Re***********@worldnet.att.netwrote in message
news:FC*********************@bgtnsc04-news.ops.worldnet.att.net...
Lionel B wrote in message ...
>>On Fri, 05 Jan 2007 20:19:02 +0100, Bo Persson wrote:
>>>Yes, I don't know what the Standard says (maybe someone can
enlighten us on this), but it doesn't work for me here on a recent
gcc. In principle I'm not sure why it *shouldn't* work, since read
and write positions are separately maintained.

Read and write positions are not separately maintained, there is just
one
"stream position". You have to do a seek to change between read and
write
modes.

Is that right? I stand corrected then (I thought seekg() and seekp()
maintained separate read/write positions simultaneously).
Lionel B

Bo's statement now has me confused[1]. Is it just a GCC thing?
Try this and see what output you get:

#include <iostream>
#include <sstream// or <fstream>
#include <string>

int main(){
using std::cout; // for NG post
std::stringstream ss( "Hello World!\n" );
stringstream maintains two seek positions, fstream maintains one.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jan 6 '07 #9

P.J. Plauger wrote in message ...
>"BobR" wrote in message...
>Bo's statement now has me confused[1]. Is it just a GCC thing?
Try this and see what output you get:
#include <iostream>
#include <sstream// or <fstream>
#include <string>
int main(){
using std::cout; // for NG post
std::stringstream ss( "Hello World!\n" );

stringstream maintains two seek positions, fstream maintains one.
Thank you.

--
Bob R
POVrookie
Jan 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephen | last post by:
could someone give me some advise on a problem I have. At present i have a web application which displays pdf files in a web browser using javascript and the window.open method and pointing to a...
2
by: gary smith | last post by:
I am trying to open a file (file --> open --> file) at an FTP location using visual Studio.net. Unfortunately, I get an "unspecified error" alert whenever I select a file. Can anyone help...
3
by: Murasama | last post by:
Hi there, Im trying a simple file IO operation in Visual Studio .NET 2003 and it can't seem to open the file. If I run the exe in the debug directory it works fine but if I click the start...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have some experience with backups in general though. The...
2
by: nissiml | last post by:
hi, i'm trying to open a asp.net web page that list files from a Windows application like winword and select a file from it . what do i have to do to make it happen, is it simple ? Thanks in...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
18
by: Coffee Pot | last post by:
Thanks for any advice. ~ CP
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.