472,954 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

fstream

Should this do something?

#include <fstream>
#include <string>

int main()
{
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
std::string s="";
filestr << s;
s = "Not " + s;
filestr >s;
return 0;
}

I test it and it doesn't, but it doesn't complain either.

How do I use fstream to read and write from a file?

Thanks.

May 30 '07 #1
6 3674
Gaijinco wrote:
Should this do something?
In what sense? Observable behavior? Most likely it will create
the file if the file doesn't exist.
>
#include <fstream>
#include <string>

int main()
{
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
std::string s="";
filestr << s;
Here you _output_ 's'.
s = "Not " + s;
filestr >s;
Here you _input_ from 'filestr' into 's'.

Are you sure you understand the meaning of the "direction" of
the operators << and >>?
return 0;
}

I test it and it doesn't, but it doesn't complain either.

How do I use fstream to read and write from a file?
You have.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 30 '07 #2
Ok. I mess up with the << and >operators using them in the wrong
places.

But, I want to have an fstream object to read something from a file,
and then write something back.

To my understanding that should be done with:

std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);

Right?

So if I want to read something I would use the >operator, like:

filestr >x;

And to write something I should use:

filestr << x

Is there any other consideration?

Because when I use this:

#include <fstream>
#include <string>

int main()
{
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
std::string s="";
filestr >s;
s = "Not " + s;
filestr << s;
return 0;
}

The file is never opened (if it doesn't exist it doesn't create it
also), if I print "s" it's still empty, and by the end of the program
the file is never written on.

May 30 '07 #3
Gaijinco wrote:
Ok. I mess up with the << and >operators using them in the wrong
places.

But, I want to have an fstream object to read something from a file,
and then write something back.
OK.
To my understanding that should be done with:

std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);

Right?
Right. You can drop the 'std::fstream::in/out', they are the default.
Have you tried looking whether any errors are reported by the stream
itself?
So if I want to read something I would use the >operator, like:

filestr >x;

And to write something I should use:

filestr << x

Is there any other consideration?
The file may not have enough room for writing back. That should
be considered... Beyond that, I am not sure. I actually never
used formatted I/O on an fstream. It's always binary where I come
from, or different files for writing than for reading (and then
renaming them after closing).
Because when I use this:

#include <fstream>
#include <string>

int main()
{
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
std::string s="";
filestr >s;
s = "Not " + s;
filestr << s;
return 0;
}

The file is never opened (if it doesn't exist it doesn't create it
also), if I print "s" it's still empty, and by the end of the program
the file is never written on.
Yes, the file has to exist if you intend to read from it, that's true.
If you intend to append to the file or write at the end of it, use
different mode flags. Beyond that I'm not very familiar with op <<
and op >specifics for read-write streams.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 30 '07 #4
Gaijinco wrote:
Ok. I mess up with the << and >operators using them in the wrong
places.

But, I want to have an fstream object to read something from a file,
and then write something back.

To my understanding that should be done with:

std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);

Right?

So if I want to read something I would use the >operator, like:

filestr >x;

And to write something I should use:

filestr << x

Is there any other consideration?

Because when I use this:

#include <fstream>
#include <string>

int main()
{
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
std::string s="";
filestr >s;
s = "Not " + s;
filestr << s;
return 0;
}

The file is never opened (if it doesn't exist it doesn't create it
also), if I print "s" it's still empty, and by the end of the program
the file is never written on.
IM(very)HO I would do something like this:

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

using namespace std;

int main()
{
ofstream file_out;
ifstream file_in;

file_out.open( "test.txt" ); // to ensure the file is there
file_out.close(); //to begin with

file_in.open( "test.txt" );
if ( !file_in.is_open() )
{
cout << "Opening file failed" << endl;
}
string s = "";
getline( file_in, s );
s = "Not " + s;
file_in.close();

file_out.open( "test.txt" );
file_out << s;
file_out.close();
return 0;
}

The fstream( ios::in ), and conversely ifstream, doesn't handle file
creation. It only tries to open what is there, and if it doesn't exist
simply gives up. fstream ( ios::out ) and ofstream DO handle file
creation. If it doesn't exsist -create it. Thus the initial opening of
file_out ensures it is there to begin with.
May 30 '07 #5

Gaijinco <ga******@gmail.comwrote in message ...
Ok. I mess up with the << and >operators using them in the wrong
places.
But, I want to have an fstream object to read something from a file,
and then write something back.
To my understanding that should be done with:

std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out);
Right?
Not if you don't understand how file IO works, and what the 'seekg()' and
'seekp()' are used for. (...and understand that the file will be truncated
at the point of write (output). also look up 'std::ios_base').
So if I want to read something I would use the >operator, like:
filestr >x;
std::ifstream filestr("myfile.txt");
if( not filestr.is_open() ){ std::cerr<<"Error"; exit(1);}
int x(0);
filestr >x;
if( not filestr ){ std::cerr<<"Error"; exit(1);}
filestr.close();
>
And to write something I should use:
filestr << x
std::ofstream filestr2("myfile.txt");
if( not filestr2.is_open() ){ std::cerr<<"Error"; exit(1);}
int x(50);
filestr2 << x;
if( not filestr2 ){ std::cerr<<"Error"; exit(1);}
filestr2.close();
>
Is there any other consideration?
Because when I use this:

#include <fstream>
#include <string>
int main(){
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
std::string s="";
filestr >s;
You can't read nothing into something. <G>
s = "Not " + s;
filestr << s;
When you read nothing above, you put the filestream at EOF or a fail-state.
Use 'filestr.clear()', then output your 's'. You should then end up with a
file which contains "Not " (unless the file already existed and had
content).
return 0;
}

The file is never opened (if it doesn't exist it doesn't create it
also), if I print "s" it's still empty, and by the end of the program
the file is never written on.
--
Bob R
POVrookie
May 30 '07 #6
On May 30, 7:34 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Gaijinco wrote:
Ok. I mess up with the << and >operators using them in the wrong
places.
But, I want to have an fstream object to read something from a file,
and then write something back.
OK.
To my understanding that should be done with:
std::fstream filestr ("test.txt", std::fstream::in |
std::fstream::out);
Right?
Right. You can drop the 'std::fstream::in/out', they are the default.
Have you tried looking whether any errors are reported by the stream
itself?
So if I want to read something I would use the >operator, like:
filestr >x;
And to write something I should use:
filestr << x
Is there any other consideration?
The file may not have enough room for writing back.
In which case it will be extended (unless the disk is full).
More likely, the read failed. In general, any time you read to
end of file, all following operations will fail unless you
intercal a call to clear().

Also, the read and write position are common. So what he's
doing will write immediately following where he last read. If
the file originally contained "toto\n", for example, after his
operations, it would contain "totoNot toto". (At least in
practice; it's quite conceivable that on some systems,
overwriting the '\n' in a text file doesn't work quite the way
you'd expect.)
That should be considered... Beyond that, I am not sure. I
actually never used formatted I/O on an fstream. It's always
binary where I come from, or different files for writing than
for reading (and then renaming them after closing).
In practice, there's one exception that works: reading to the
end of file, then clearing the error bit, and writing
(appending) more data. Other than that, if you're going to be
seeking, you'll probably want to use binary---most of the time,
I will, in fact, use the system level interface: open, read,
write, lseek and close in my case.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 31 '07 #7

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

Similar topics

6
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
3
by: David Blasdell | last post by:
This appears very strange to me, I'm having a problem with a fstream object inside a class which is not being called directly from main (another class calls the class that contains the fstream...
3
by: ratzeel | last post by:
The following snippet code throws an error while compiling on SUN os.. Any idea how to resolve this... #include <iostream.h> #include <fstream.h> #include <math.h> #include <algorithm>...
9
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
1
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
5
by: ehui928 | last post by:
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> ...
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;
5
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.