C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to insert datas into a fstrem data

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Constructor
Guest





PostPosted: Thu Aug 28, 2003 3:09 pm    Post subject: How to insert datas into a fstrem data Reply with quote



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





PostPosted: Fri Aug 29, 2003 11:26 am    Post subject: Re: How to insert datas into a fstrem data Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.