add bytes to file

B

Bill Cunningham

Can someone show me how to add 3 or so bytes to the end of a file? I
have been studying and I'm thinking fseek() and fgetc() might be involved. I
can't seem to find any examples online unless I'm overlooking something.
Nothing in C that is.

Bill
 
L

Luuk

Can someone show me how to add 3 or so bytes to the end of a file? I
have been studying and I'm thinking fseek() and fgetc() might be involved. I
can't seem to find any examples online unless I'm overlooking something.
Nothing in C that is.

Bill

if you know how to do some c programming,
than googling for:
c append to file
should solve your problem.
 
A

An tSin Gorm

Bill Cunningham said:
Can someone show me how to add 3 or so bytes to the end of a file? I
have been studying and I'm thinking fseek() and fgetc() might be involved. I
can't seem to find any examples online unless I'm overlooking something.
Nothing in C that is.

FILE *f = fopen(path, "r+");
fseek(f, 0, SEEK_END);
fputs("3 or so bytes", f);
fclose(f);
 
B

Bill Cunningham

An said:
FILE *f = fopen(path, "r+");
fseek(f, 0, SEEK_END);
fputs("3 or so bytes", f);
fclose(f);

Sorry I meant fputc but I guess fputs would work.

Bill
 
B

Bill Cunningham

Luuk said:
if you know how to do some c programming,
than googling for:
c append to file
should solve your problem.

Ok but I just want to make sure I am not appending a string or any text
data. I want to append '\0' to the end and be able to reverse it. This is
for an encryption project.

Bill
 
B

Bill Cunningham

An said:
Or fprintf or fwrite.

I'm kinda scratching my head here. Does you code append textual strings? I
want to append '\0' to the end of the file and be able to reverse that
process. Iprobably could go ahead and use a+ instead of r+ and skip the
fseek.

Bill
 
A

An tSin Gorm

Bill Cunningham said:
I'm kinda scratching my head here. Does you code append textual strings? I
want to append '\0' to the end of the file and be able to reverse that
process. Iprobably could go ahead and use a+ instead of r+ and skip the
fseek.

I'm used to unix so text and binary files are the same; you can add "b" if
appropriate. Once the file is positionned, you can write the bytes with whatever
technique will write the bytes.
 
B

Ben Bacarisse

Bill Cunningham said:
Ok but I just want to make sure I am not appending a string or any text
data. I want to append '\0' to the end and be able to reverse it. This is
for an encryption project.

If it relates to the programs you've been posting in
comp.unix.programmer this is the wrong way to pad the input so that it's
a multiple of 8 bytes in size.
 
B

Bill Cunningham

An said:
Or fprintf or fwrite.

Hum. I see fputs takes a char *. I don't think that will be what I'm looking
for. And fputc only passes one int which would be as '\0' ascii equivalent I
think I might need fwrite().

Bill
 
B

Bill Cunningham

An said:
I'm used to unix so text and binary files are the same; you can add
"b" if appropriate. Once the file is positionned, you can write the
bytes with whatever technique will write the bytes.

I am in the same boat as far as binary and text being treated the same. I
use linux.

Bill
 
B

Bill Cunningham

Ben said:
If it relates to the programs you've been posting in
comp.unix.programmer this is the wrong way to pad the input so that
it's a multiple of 8 bytes in size.

Yes. Ok I think I need just a simple example of how to pad for the unix
function using C and just that. Once I get a file of a multiple of 8 I can
go ahead and encrypt. padding and encrypting together right now confuses me.


Bill
 
B

Ben Bacarisse

Bill Cunningham said:
Yes. Ok I think I need just a simple example of how to pad for the unix
function using C and just that. Once I get a file of a multiple of 8 I can
go ahead and encrypt. padding and encrypting together right now
confuses me.

I outlined one way in the comp.unix.programmer where this question also
appeared.
 
B

Barry Schwarz

Hum. I see fputs takes a char *. I don't think that will be what I'm looking

Since you haven't told us what you really want to do, how will anyone
know. If the 3 bytes do not contain a '\0', fputs will work fine.
for. And fputc only passes one int which would be as '\0' ascii equivalent I

While the argument to fputc is an int, it only writes a byte to the
file. You would need to call it three times. And in what way do you
suppose an int would resemble a '\0'?
think I might need fwrite().

Do you want to write three bytes or a number of bytes which might on
occasion be three? Is the number of bytes known at compile time or is
computed at run time? Ditto for the content of the bytes? Is there
an upper limit to the number bytes you might ever want to write? Is
the file already open? If so, is it open for input or output? Is the
file being processed by code you control or are you running someone
else's library?
 
B

Bill Cunningham

Barry said:
Since you haven't told us what you really want to do, how will anyone
know. If the 3 bytes do not contain a '\0', fputs will work fine.

I want null bytes.
While the argument to fputc is an int, it only writes a byte to the
file. You would need to call it three times. And in what way do you
suppose an int would resemble a '\0'?

The ascii numberic equivalant of NUL.
Do you want to write three bytes or a number of bytes which might on
occasion be three? Is the number of bytes known at compile time or is
computed at run time? Ditto for the content of the bytes? Is there
an upper limit to the number bytes you might ever want to write? Is
the file already open? If so, is it open for input or output? Is the
file being processed by code you control or are you running someone
else's library?

See "glibc error" in comp.unix.programmer for what I've been trying to do.
I've been working with an OT function called cbc_crypt().
 
B

Barry Schwarz

snip


I want null bytes.

So why didn't you say so in the first place.
The ascii numberic equivalant of NUL.

And which ASCII character is that?
See "glibc error" in comp.unix.programmer for what I've been trying to do.
I've been working with an OT function called cbc_crypt().

And the reason you changed newgroups without presenting a complete
description is ...
 
G

Greg Martin

Hum. I see fputs takes a char *. I don't think that will be what I'm looking
for. And fputc only passes one int which would be as '\0' ascii equivalent I
think I might need fwrite().

Bill

There's lots of ways to accomplish this. This would work. Whether it's
what you need is another matter.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
FILE* out;
char bytes[] = { '\0', '\0', '\0' };
int sz = sizeof (bytes) / sizeof (bytes[0]);
int nw;

if (argc < 2) {
return EXIT_FAILURE;
}

if ((out = fopen (argv[1], "a")) != NULL) {
nw = fwrite (bytes, sizeof (bytes[0]), sz, out);
if (nw != sz) {
perror ("fwrite");
}

fclose (out);
}

return 0;
}
 
B

Ben Bacarisse

Barry Schwarz said:
And which ASCII character is that?

It's the character with code zero. In the very first version of ASCII
it was called NULL, but by 1967 all the control codes (< 32) had
been given two- or three-character names and it had become NUL.

Since you probably know this, I suspect you are making so other
pedagogic point and I've just missed it!

<snip>
 
J

Jorgen Grahn

FILE *f = fopen(path, "r+");
fseek(f, 0, SEEK_END);
fputs("3 or so bytes", f);
fclose(f);

Why not this?

FILE *f = fopen(path, "a");
fputs("3 or so bytes", f);
fclose(f);

Plus error checking of course.

/Jorgen
 
K

Keith Thompson

Greg Martin said:
There's lots of ways to accomplish this. This would work. Whether it's
what you need is another matter.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {
FILE* out;
char bytes[] = { '\0', '\0', '\0' };
int sz = sizeof (bytes) / sizeof (bytes[0]);
int nw;

if (argc < 2) {
return EXIT_FAILURE;
}

if ((out = fopen (argv[1], "a")) != NULL) {
nw = fwrite (bytes, sizeof (bytes[0]), sz, out);
if (nw != sz) {
perror ("fwrite");
}

fclose (out);
}

return 0;
}

fopen()'s mode "a" is *text* append mode:

append; open or create text file for writing at end-of-file

If you write characters other than printable characters, '\n', and
'\t' to a text stream, they won't necessarily appear when you read
them back; see N1370 7.21.2p2 for details.

If you're writing 0 bytes to a file, you (obviously, I think)
need to do it in binary mode, probably by passing "ab" as the mode
argument to fopen().

Even that's not guaranteed to work since a binary stream "may,
however, have an implementation-defined number of null characters
appended to the end of the stream" (N1370 7.21.2p3). This would
most likely apply to a system that tracks sizes of binary files
in blocks rather than bytes. It almost certainly wouldn't be a
concern for any system the OP is likely to be using.
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top