By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

how to insert tow byte in front of a file

P: n/a
(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


Jun 18 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Gnafu wrote:
(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..


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.

Jun 18 '06 #2

P: n/a
In article <e7*************@news.t-online.com>,
ra******@t-online.de says...

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

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 18 '06 #3

P: n/a
> 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..
Jun 18 '06 #4

P: n/a
Jerry Coffin wrote:
In article <e7*************@news.t-online.com>,
ra******@t-online.de says...

[ ... ]
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 ;-)

Jun 18 '06 #5

P: n/a
* Rolf Magnus:
Jerry Coffin wrote:
In article <e7*************@news.t-online.com>,
ra******@t-online.de says...

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

--
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?
Jun 18 '06 #6

P: n/a
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);

Gnafu wrote:
(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


Jun 18 '06 #7

P: n/a
please don't top-post.

hdante wrote:
Yo> Gnafu wrote:
(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?


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.
Jun 19 '06 #8

P: n/a
What does "top-post" mean ?

I'm sorry, I thought that's what he wanted.

Rolf Magnus wrote:
please don't top-post.

hdante wrote:
Yo> Gnafu wrote:
(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?


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.


Jun 19 '06 #9

P: n/a
hdante wrote:
Rolf Magnus wrote:
please don't top-post.

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.

Jun 19 '06 #10

P: n/a
hdante <hd****@gmail.com> wrote:
What does "top-post" mean ?


http://en.wikipedia.org/wiki/Top_posting

Your responses should be interspersed thoughout the trimmed quoted text.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jun 19 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.