add bytes to file

Discussion in 'C Programming' started by Bill Cunningham, Sep 30, 2012.

  1. 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
     
    Bill Cunningham, Sep 30, 2012
    #1
    1. Advertising

  2. Bill Cunningham

    Luuk Guest

    On 30-09-2012 21:40, Bill Cunningham 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.
    >
    > Bill
    >
    >


    if you know how to do some c programming,
    than googling for:
    c append to file
    should solve your problem.
     
    Luuk, Sep 30, 2012
    #2
    1. Advertising

  3. Bill Cunningham

    An tSin Gorm Guest

    In article <k4a768$t96$>,
    "Bill Cunningham" <> 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);

    --
    My name Indigo Montoya. \\ Annoying Usenet one post at a time.
    You flamed my father. \' At least I can stay in character.
    Prepare to be spanked. // When you look into the void,
    Stop posting that! `/ the void looks into you, and fulfills you.
     
    An tSin Gorm, Sep 30, 2012
    #3
  4. An tSin Gorm wrote:
    > In article <k4a768$t96$>,
    > "Bill Cunningham" <> 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
     
    Bill Cunningham, Sep 30, 2012
    #4
  5. Luuk wrote:

    > 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
     
    Bill Cunningham, Sep 30, 2012
    #5
  6. Bill Cunningham

    An tSin Gorm Guest

    In article <k4a9do$3t7$>,
    "Bill Cunningham" <> wrote:

    > An tSin Gorm wrote:
    > > In article <k4a768$t96$>,
    > > "Bill Cunningham" <> 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.

    --
    My name Indigo Montoya. \\ Annoying Usenet one post at a time.
    You flamed my father. \' At least I can stay in character.
    Prepare to be spanked. // When you look into the void,
    Stop posting that! `/ the void looks into you, and fulfills you.
     
    An tSin Gorm, Sep 30, 2012
    #6
  7. An tSin Gorm wrote:
    > In article <k4a9do$3t7$>,
    > "Bill Cunningham" <> wrote:
    >
    >> An tSin Gorm wrote:
    >>> In article <k4a768$t96$>,
    >>> "Bill Cunningham" <> 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.


    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
     
    Bill Cunningham, Sep 30, 2012
    #7
  8. Bill Cunningham

    An tSin Gorm Guest

    In article <k4aa6c$5u3$>,
    "Bill Cunningham" <> wrote:

    > An tSin Gorm wrote:
    > > In article <k4a9do$3t7$>,
    > > "Bill Cunningham" <> wrote:
    > >
    > >> An tSin Gorm wrote:
    > >>> In article <k4a768$t96$>,
    > >>> "Bill Cunningham" <> 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.

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

    --
    My name Indigo Montoya. \\ Annoying Usenet one post at a time.
    You flamed my father. \' At least I can stay in character.
    Prepare to be spanked. // When you look into the void,
    Stop posting that! `/ the void looks into you, and fulfills you.
     
    An tSin Gorm, Sep 30, 2012
    #8
  9. "Bill Cunningham" <> writes:

    > Luuk wrote:
    >
    >> 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.


    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.

    --
    Ben.
     
    Ben Bacarisse, Sep 30, 2012
    #9
  10. An tSin Gorm wrote:
    > In article <k4a9do$3t7$>,
    > "Bill Cunningham" <> wrote:
    >
    >> An tSin Gorm wrote:
    >>> In article <k4a768$t96$>,
    >>> "Bill Cunningham" <> 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
    for. And fputc only passes one int which would be as '\0' ascii equivalent I
    think I might need fwrite().

    Bill
     
    Bill Cunningham, Sep 30, 2012
    #10
  11. 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
     
    Bill Cunningham, Sep 30, 2012
    #11
  12. 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
     
    Bill Cunningham, Sep 30, 2012
    #12
  13. "Bill Cunningham" <> 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.
     
    Ben Bacarisse, Sep 30, 2012
    #13
  14. On Sun, 30 Sep 2012 16:40:35 -0400, "Bill Cunningham"
    <> wrote:

    >An tSin Gorm wrote:
    >> In article <k4a9do$3t7$>,
    >> "Bill Cunningham" <> wrote:
    >>
    >>> An tSin Gorm wrote:
    >>>> In article <k4a768$t96$>,
    >>>> "Bill Cunningham" <> 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
     
    Barry Schwarz, Sep 30, 2012
    #14
  15. Barry Schwarz wrote:
    > On Sun, 30 Sep 2012 16:40:35 -0400, "Bill Cunningham"
    > <> wrote:
    >
    >> An tSin Gorm wrote:
    >>> In article <k4a9do$3t7$>,
    >>> "Bill Cunningham" <> wrote:
    >>>
    >>>> An tSin Gorm wrote:
    >>>>> In article <k4a768$t96$>,
    >>>>> "Bill Cunningham" <> 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().
     
    Bill Cunningham, Sep 30, 2012
    #15
  16. On Sun, 30 Sep 2012 17:27:44 -0400, "Bill Cunningham"
    <> wrote:

    >Barry Schwarz wrote:
    >> On Sun, 30 Sep 2012 16:40:35 -0400, "Bill Cunningham"
    >> <> 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
     
    Barry Schwarz, Oct 1, 2012
    #16
  17. Bill Cunningham

    Greg Martin Guest

    On 12-09-30 01:40 PM, Bill Cunningham wrote:
    >>>> In article <k4a768$t96$>,
    >>>> "Bill Cunningham" <> 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;
    }
     
    Greg Martin, Oct 1, 2012
    #17
  18. Barry Schwarz <> 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.
     
    Ben Bacarisse, Oct 1, 2012
    #18
  19. Bill Cunningham

    Jorgen Grahn Guest

    On Sun, 2012-09-30, An tSin Gorm wrote:
    > In article <k4a768$t96$>,
    > "Bill Cunningham" <> 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 .
     
    Jorgen Grahn, Oct 1, 2012
    #19
  20. Greg Martin <> 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.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"
     
    Keith Thompson, Oct 1, 2012
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Collins
    Replies:
    3
    Views:
    6,073
    Jason Collins
    Feb 18, 2004
  2. mrby

    4-bytes or 8-bytes alignment?

    mrby, Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    442
    Mark McIntyre
    Nov 2, 2004
  3. Replies:
    5
    Views:
    568
    Flash Gordon
    Apr 9, 2006
  4. Yandos
    Replies:
    12
    Views:
    5,149
    Pete Becker
    Sep 15, 2005
  5. Replies:
    8
    Views:
    522
    Bob Hairgrove
    Apr 10, 2006
Loading...

Share This Page