|
|
 |
 |
 |
 |
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.
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
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.
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
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.
Gaijinco <gaiji @gmail.com> wrote 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
On May 30, 7:34 pm, "Victor Bazarov" <v.Abaza@comAcast.net> wrote:
> 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:james.ka@gmail.com Conseils en informatique oriente objet/ Beratung in objektorientierter Datenverarbeitung 9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
|
 |
 |
 |
 |
|