 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gnafu Guest
|
Posted: Mon Jun 19, 2006 4:05 pm Post subject: how to insert tow byte in front of a file |
|
|
(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
|
Posted: Mon Jun 19, 2006 6:57 pm Post subject: Re: how to insert tow byte in front of a file |
|
|
* "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
|
Posted: Tue Jun 20, 2006 6:44 pm Post subject: Re: how to insert tow byte in front of a file |
|
|
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 |
|
 |
|
|
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
|
|