Question about fstream operator >> and <<

N

neowillis

code:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
fstream iofile;
string ss;

iofile.open("a.txt", fstream::in | fstream::eek:ut);

iofile.seekg(ios::beg);
iofile >> ss;
cout << ss << endl;

iofile.seekp(ios.beg);
iofile << "abcdefg" << endl;
iofile << flush;

iofile.close();

return 0;
}

Writing to the file is failed, why?
I swapped the position of the reading and writing operations, writing
is succeeded.
 
B

Bo Persson

(e-mail address removed) wrote:
:: code:
::
:: #include <iostream>
:: #include <fstream>
:: #include <string>
::
:: using namespace std;
::
:: int main()
:: {
:: fstream iofile;
:: string ss;
::
:: iofile.open("a.txt", fstream::in | fstream::eek:ut);
::
:: iofile.seekg(ios::beg);
:: iofile >> ss;
:: cout << ss << endl;
::
:: iofile.seekp(ios.beg);
:: iofile << "abcdefg" << endl;
:: iofile << flush;
::
:: iofile.close();
::
:: return 0;
:: }
::
:: Writing to the file is failed, why?
:: I swapped the position of the reading and writing operations, writing
:: is succeeded.

How much text do you have in the file to start with? If your input reaches
end-of-file, that condition will stick until you do an iofile.clear().


Bo Persson
 
B

BobR

code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream iofile;
std::string ss;
iofile.open("a.txt", fstream::in | fstream::eek:ut);
iofile.seekg(ios::beg);
iofile >> ss;
cout << ss << endl;
iofile.seekp(ios.beg);
iofile << "abcdefg" << endl;
iofile << flush;
iofile.close();
return 0;
}

Writing to the file is failed, why?
I swapped the position of the reading and writing operations, writing
is succeeded.

How do you know it failed?

#include <iostream>
#include <fstream>
#include <string>
int main(){
using std::cout; // for NG post
using std::cerr; // for NG post
{ // scope1
std::eek:fstream iofile( "a.txt" ); // create file
if( not iofile.is_open() ){
cerr<<"\n fstream FAILED 0"<<std::endl;
exit( EXIT_FAILURE );
}
iofile.close();
} // scope1

std::fstream iofile( "a.txt" ); // std::ios_base::in|std::ios_base::eek:ut
if( not iofile.is_open() ){
cerr<<"\n fstream FAILED 1"<<std::endl;
exit( EXIT_FAILURE );
}
iofile << "abcdefg" <<std::endl;
if( not iofile ){
cerr<<"\n fstream FAILED 2"<<std::endl;
exit( EXIT_FAILURE );
}

iofile.seekg( 0, std::ios::beg );
string ss;
iofile >> ss;
if( not iofile.good() ){ // another way
cerr<<"\n fstream FAILED 3"<<std::endl;
exit( EXIT_FAILURE );
}
cout << ss << std::endl;

iofile.close();
return 0; // EXIT_SUCCESS
} // main() end

// output: abcdefg


Experts: Never ever tell someone they don't need to use .close() on a file
because it is going out-of-scope!! In testing the above (in my TestBench
program), I had it write "abcdefg" to another file which was in it's own
scope, inside it's own (class) method, inside a class which was locally
instantiated in it's own scope, and (file) named differently! Yeah, there
are some weird circumstances involved (MinGW(GCC3.3.1), wxWidgets 2.8.0,
many streams open, win98se), BUT, it did happen! Just be warned.
I'll report back here if I find how/why it happened (*all* my .rdbuf() tests
were commented-out, no low-level stuff active). Very strange!
 
V

Victor Bazarov

BobR said:
[..]
Experts: Never ever tell someone they don't need to use .close() on a
file because it is going out-of-scope!! [..]

So, should I first define a default-constructed fstream and only then
..open() it?

And in case the implementation I have is buggy, should I also work
around any potential problems by using only built-in types and
'if/else/goto' (in case there is a problem with 'switch/while/...'
or with user-defined types)? Just wondering...

V
 
B

BobR

Victor Bazarov said:
BobR said:
[..]
Experts: Never ever tell someone they don't need to use .close() on a
file because it is going out-of-scope!! [..]

So, should I first define a default-constructed fstream and only then
.open() it?
And in case the implementation I have is buggy, should I also work
around any potential problems by using only built-in types and
'if/else/goto' (in case there is a problem with 'switch/while/...'
or with user-defined types)? Just wondering...

No, no and no. <G>

I have not investigated yet. But, I did put file.close() [which puts the
stream in a fail-state] on the 'distant' file and it didn't write to it. I
think it's something in the newer (2.8.0) wxWidgets that's messing with the
stream(s) states. It might be something I did or didn't enable when building
the wx library.
Of course we can't discuss wxWidgets here, but, a file stream created in one
scope should not mess with a file stream created in another scope (two
different file names). My TestBench program is cluttered (again) with
hundreds of little test-snippets, so maybe while 'housecleaning' I'll spot
where things got crossed (<crosses fingers>). I may just go back to my older
wxWidgets (since the executable size doubled just by linking to the newer
version), and test the file-cross thing again.

Both files were opened using 'fstream' ( no i or o), and are the only two
opened that way in the whole project ( all the rest use ifstream or
ofstream).

If you (or anyone) have something specific to look for, I'd appreciate a
response. Thanks.

Thanks for listening to my idiotic rants. <G> Back to work for me.
 
J

James Kanze

BobR said:
[..]
Experts: Never ever tell someone they don't need to use .close() on a
file because it is going out-of-scope!! [..]
So, should I first define a default-constructed fstream and only then
.open() it?

That's a question of style. The important thing is that
regardless of whether you pass the filename directly to the
constructor, or call open explicitly, you check whether it has
succeeded. Similarly, the important thing is that after
actually closing the file, you check whether it succeeded or
not. Which, of course, you can't do if you close the file in
the destructor.j

There are some exceptions. For example, if you're processing an
error, which means that the contents of the file are invalid
anyway, and that the file will be immediately removed, it really
doesn't matter whether the flush in the close worked or not.
And the issue is usually much less critical for input; I don't
always bother closing input files explicitly.

I might add that if you're outputting to cout, you should flush
it and check the status before returning as well.
And in case the implementation I have is buggy, should I also work
around any potential problems by using only built-in types and
'if/else/goto' (in case there is a problem with 'switch/while/...'
or with user-defined types)? Just wondering...

What on earth for? Write errors are a fact of life, and can and
must be handled. Where as you really have to assume that your
implementation is working correctly; if e.g. main memory fails
(it's happened to me), there's not much a program can do about
it. (And of course, unlike write errors, it's pretty rare.)
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top