how to insert tow byte in front of a file

G

Gnafu

(Hi to all... ^_^)

I tryed theese ways:

fstream riaperto( "codici.dat", ios::eek:ut|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::eek:ut|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
 
R

Rolf Magnus

Gnafu said:
(Hi to all... ^_^)

I tryed theese ways:

fstream riaperto( "codici.dat", ios::eek:ut|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::eek:ut|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..

Write the new bytes you want to a temporary file, then copy the content of
the original file over. If that was successful, rename the temporary file
to the name of the original.
 
J

Jerry Coffin

[ ... ]
Write the new bytes you want to a temporary file, then copy the content of
the original file over. If that was successful, rename the temporary file
to the name of the original.

....and hope there are no other hard links to the file --
because if there are, you now have two separate files,
and all of the other hard links point to the unmodified
file.
 
G

Gnafu

Write the new bytes you want to a temporary file, then copy the content of
the original file over. If that was successful, rename the temporary file
to the name of the original.

I wanted to not do that.... :(
Ok, thank anyway..
 
R

Rolf Magnus

Jerry said:
[ ... ]
Write the new bytes you want to a temporary file, then copy the content
of the original file over. If that was successful, rename the temporary
file to the name of the original.

...and hope there are no other hard links to the file --
because if there are, you now have two separate files,
and all of the other hard links point to the unmodified
file.

That's one of the reasons to better use symlinks ;-)
 
A

Alf P. Steinbach

* Rolf Magnus:
Jerry said:
[ ... ]
Write the new bytes you want to a temporary file, then copy the content
of the original file over. If that was successful, rename the temporary
file to the name of the original.
...and hope there are no other hard links to the file --
because if there are, you now have two separate files,
and all of the other hard links point to the unmodified
file.

That's one of the reasons to better use symlinks ;-)

Depends on the OS. Windows supports hardlinks for files, symlinks for
directories, and shortcuts (which are like symlinks but not
automatically resolved) for anything. The rename approach might be
appropriate when safety against file content destruction is important,
you're guaranteed that no other process will create that file in the
span between its deletion and recreation by your program, and efficiency
is valued more than link preservation.

I think it should be a user-selectable option.

Follow-ups to [comp.programming].
 
H

hdante

You'll need to put "POSIX compliant system" on your product
requirements box for doing this:

i = open ("blarg", O_WRONLY);
lseek(i, 0, SEEK_SET);
write(i, my_buffer, 2);
close(i);
 
R

Rolf Magnus

please don't top-post.
u'll need to put "POSIX compliant system" on your product
requirements box for doing this:

i = open ("blarg", O_WRONLY);
lseek(i, 0, SEEK_SET);
write(i, my_buffer, 2);
close(i);

This will not insert two bytes at the beginning. Rather it overwrites the
first two bytes.
 
R

Rolf Magnus

hdante said:
What does "top-post" mean ?

It means that you are writing your text above the text you're answering to
instead of in a chronological order. I'll quote from Alf P. Steinbach's
signature:

A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

You should write your answers below the original text and directly below the
part that you are answering to. Also snip away parts that are not relevant
to your answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top