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 tow byte in front of a file

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





PostPosted: Mon Jun 19, 2006 4:05 pm    Post subject: how to insert tow byte in front of a file Reply with quote



(Hi to all... ^_^)

I tryed theese ways:

fstream riaperto( "codici.dat", ios::out|ios::binary|ios::app);
if(riaperto.bad()){
cerr<<"errore di apertura"<<endl;
system("Pause");
}

riaperto.seekp(ios::beg);
riaperto.put(char(nbyte/256));
riaperto.put(char(nbyte%256));

^= This one writes the bytes at the end of the file (allright, it's the
"app") .

ofstream riaperto( "codici.dat", ios::out|ios::binary);
if(riaperto.bad()){
cerr<<"errore di apertura"<<endl;
system("Pause");
}

riaperto.seekp(ios::beg);
riaperto.put(char(nbyte/256));
riaperto.put(char(nbyte%256));

This one it writes at the beginning but, then, the file contains only that
tow bytes... (overwrite)

What can i do?

Thanks in advance..

Gnafu





[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ralf Fassel
Guest





PostPosted: Mon Jun 19, 2006 6:57 pm    Post subject: Re: how to insert tow byte in front of a file Reply with quote



* "Gnafu" <chiedimela (AT) in (DOT) news.ciaociao>
| This one it writes at the beginning but, then, the file contains
| only that tow bytes... (overwrite)
|
| What can i do?

See
http://www.faqs.org/faqs/C-faq/faq/
In there, topics
12.30 I'm trying to update a file in place, by using fopen mode "r+",
reading a certain string, and writing back a modified string,
but it's not working.
and
19.14 How can I insert or delete a line (or record) in the middle of a
file?

R'

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Tue Jun 20, 2006 6:44 pm    Post subject: Re: how to insert tow byte in front of a file Reply with quote



Gnafu wrote:

To start with, what's a "tow byte"? And what are you really
trying to do: insert an additional byte at the start of the
file, or replace the existing byte.

Quote:
I tryed theese ways:

fstream riaperto( "codici.dat", ios::out|ios::binary|ios::app);
if(riaperto.bad()){
cerr<<"errore di apertura"<<endl;
system("Pause");
}

riaperto.seekp(ios::beg);
riaperto.put(char(nbyte/256));
riaperto.put(char(nbyte%256));

^= This one writes the bytes at the end of the file (allright,
it's the "app") .

That's the definition of ios::app---all writes go to the end of
file. (Atomically, if possible.)

Quote:
ofstream riaperto( "codici.dat", ios::out|ios::binary);

This truncates the file.

Quote:
if(riaperto.bad()){
cerr<<"errore di apertura"<<endl;
system("Pause");
}

riaperto.seekp(ios::beg);
riaperto.put(char(nbyte/256));
riaperto.put(char(nbyte%256));

This one it writes at the beginning but, then, the file
contains only that tow bytes... (overwrite)

What can i do?

What do you want to do? To overwrite bytes in a file, you need
to open it for bidirectional access: ios::in|ios::out. If you
want to insert bytes, however, you can't without copying the
entire file.

--
James Kanze GABI Software
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


[ 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.