Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > add bytes to file

Reply
Thread Tools

add bytes to file

 
 
Bill Cunningham
Guest
Posts: n/a
 
      09-30-2012
An tSin Gorm wrote:

> 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


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      09-30-2012
Ben Bacarisse wrote:

> 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


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-30-2012
"Bill Cunningham" <(E-Mail Removed)> writes:

> Ben Bacarisse wrote:
>
>> 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.


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

--
Ben.
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      09-30-2012
On Sun, 30 Sep 2012 16:40:35 -0400, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>An tSin Gorm wrote:
>> In article <k4a9do$3t7$(E-Mail Removed)>,
>> "Bill Cunningham" <(E-Mail Removed)> wrote:
>>
>>> An tSin Gorm wrote:
>>>> In article <k4a768$t96$(E-Mail Removed)>,
>>>> "Bill Cunningham" <(E-Mail Removed)> wrote:
>>>>
>>>>> 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);
>>>
>>> Sorry I meant fputc but I guess fputs would work.
>>>
>>> Bill

>>
>> Or fprintf or fwrite.

>
>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?

--
Remove del for email
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      09-30-2012
Barry Schwarz wrote:
> On Sun, 30 Sep 2012 16:40:35 -0400, "Bill Cunningham"
> <(E-Mail Removed)> wrote:
>
>> An tSin Gorm wrote:
>>> In article <k4a9do$3t7$(E-Mail Removed)>,
>>> "Bill Cunningham" <(E-Mail Removed)> wrote:
>>>
>>>> An tSin Gorm wrote:
>>>>> In article <k4a768$t96$(E-Mail Removed)>,
>>>>> "Bill Cunningham" <(E-Mail Removed)> wrote:
>>>>>
>>>>>> 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);
>>>>
>>>> Sorry I meant fputc but I guess fputs would work.
>>>>
>>>> Bill
>>>
>>> Or fprintf or fwrite.

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


I want null bytes.

>> 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'?


The ascii numberic equivalant of NUL.

>> 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?


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().


 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      10-01-2012
On Sun, 30 Sep 2012 17:27:44 -0400, "Bill Cunningham"
<(E-Mail Removed)> wrote:

>Barry Schwarz wrote:
>> On Sun, 30 Sep 2012 16:40:35 -0400, "Bill Cunningham"
>> <(E-Mail Removed)> wrote:
>>


snip

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


So why didn't you say so in the first place.

>>> 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'?

>
> The ascii numberic equivalant of NUL.


And which ASCII character is that?

>>> 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?

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

--
Remove del for email
 
Reply With Quote
 
Greg Martin
Guest
Posts: n/a
 
      10-01-2012
On 12-09-30 01:40 PM, Bill Cunningham wrote:
>>>> In article <k4a768$t96$(E-Mail Removed)>,
>>>> "Bill Cunningham" <(E-Mail Removed)> wrote:
>>>>
>>>>> 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.
>>>>

> 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;
}

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-01-2012
Barry Schwarz <(E-Mail Removed)> writes:

> On Sun, 30 Sep 2012 17:27:44 -0400, "Bill Cunningham"

<snip>
>> The ascii numberic equivalant of NUL.

>
> 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>
--
Ben.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-01-2012
On Sun, 2012-09-30, An tSin Gorm wrote:
> In article <k4a768$t96$(E-Mail Removed)>,
> "Bill Cunningham" <(E-Mail Removed)> wrote:
>
>> 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);


Why not this?

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

Plus error checking of course.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-01-2012
Greg Martin <(E-Mail Removed)> writes:
[...]
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
Why file containing 256 bytes is 257 bytes long? Yandos C++ 12 09-14-2005 11:53 PM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM



Advertisments