 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Constructor Guest
|
Posted: Thu Aug 28, 2003 3:09 pm Post subject: How to insert datas into a fstrem data |
|
|
Hi all,
I just want to insert datas into a file, and i use fstream to open file,
but the openmode I use does'not works well, just like this
#include <fstream>
#include <iostream>
main( )
{
using namespace std;
fstream x( "iotest.txt", ios_base::out | ios_base::ate );
x.seekp(2,ios::beg) ;
x << "testing";
}
//iotest.txt has some data, like this:
//"this is a test"
//i just want to insert "testing" into the file, not append it to the end of
the file
also I test with the openmode "ios_base::app", but didn't get what i need.
of course, i can resolve it with fopen in c-style well, but i just want to
do it
in c++ style.
Best Regrads
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Fri Aug 29, 2003 11:26 am Post subject: Re: How to insert datas into a fstrem data |
|
|
Constructor wrote:
| Quote: | I just want to insert datas into a file, and i use fstream to open file,
but the openmode I use does'not works well, just like this
|
Just FYI: 'data' is the plural of datum.
| Quote: | #include <fstream
#include
|
You also need
most compilers though.
| Quote: | main( )
{
using namespace std;
fstream x( "iotest.txt", ios_base::out | ios_base::ate );
|
Alternatively use ofstream, you can then drop the 'ios_base::out'. Also
missing here:
if(!x)
{
cerr << "main(): failed to open file for writingn";
return -1;
}
| Quote: | x.seekp(2,ios::beg) ;
x << "testing";
}
//iotest.txt has some data, like this:
//"this is a test"
//i just want to insert "testing" into the file, not append it to the end
of the file
|
Inserting will truncate the file. Inserting like in an editor is not
possible like that. Also I wonder
1. what happens, i.e. what are the results afterwards ?
2. what did you expect to be the file's content in the end ?
3. what happens if you remove the seekp() or the ios_base::ate ?
4 which compiler/stdlib do you use ? (there are still broken
implementations out there)
cheers
Uli
--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|