473,508 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3718
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
3203
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
584
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
2606
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
5268
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
5334
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
3933
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
17100
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
4146
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
9930
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...
0
7120
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...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7494
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
5626
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 project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.