fwrite error: bad address

This is a discussion on fwrite error: bad address within the C Programming forums, part of the General Programming Boards category; Hi everybody! I'm having a silly though extremely annoying problem when using fwrite. What I'm trying to do is to ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    15

    fwrite error: bad address

    Hi everybody! I'm having a silly though extremely annoying problem when using fwrite. What I'm trying to do is to calculate a random size for the message I'm about to write (although this size is limited by SIZEMAX), create a temporary file to storage it (my message) and finally write the message in it. This message consists of a series of identical characters, whose nature will depend on the value of 'i'. Now, the thing is I usually get an error (sometimes, funnily enough, it works (or so it seems)) in fwrite: "Bad address". Why is this? How can I fix it? Any idea?

    Here's my code, BTW (the comments are in Spanish, but they tell the same I just explained):

    Code:
    //calculamos el tamaño de forma aleatoria, con límite en SIZEMAX
    sizemsg = random() % SIZEMAX;
    //lo guardamos en un archivo temporal
    if((str_temp = tmpfile()) == NULL){
        perror("error al crear el fichero temporal del cliente (tmpfile)");
        exit(-1);
    }
    //introducimos el mensaje en el archivo temporal
    letra[0] = 'a' + i;
    if(fwrite(letra, 1, sizemsg, str_temp) != sizemsg){
        perror("error al escribir en el archivo temporal del cliente (fwrite)");
        exit(-1);
    }
    Just another thing: 'letra' is a char array of length 1.

    Thanks a lot!

    P.T. Sorry if I messed up grammar or spelling (if I had, feel free to correct me), English is not my first language.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by icebabe
    Just another thing: 'letra' is a char array of length 1.
    In the worst case you write SIZEMAX-1 char's starting from letra[0]. -> segfault.
    Make letra holding SIZEMAX chars.

    Kurt
    Last edited by ZuK; 05-06-2006 at 05:29 AM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    15
    But the reason I create a temporary file to storage the message is that it's too big to storage it in a variable (you can storage it in a variable, of course, but it will waste a lot of memory), so I'm afraid that isn't an option for me.

    And about...
    In the worst case you write SIZEMAX-1 char's starting from letra[0]. -> segfault.
    Are you sure that's what's gonna happen? I thought it would storage SIZEMAX copies of letra. Maybe you're thinking about fwrite(letra, sizemsg, 1, str_temp) instead of my fwrite(letra, 1, sizemsg, str_temp) ? Let me know what you think.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Yes I am shure
    Code:
      int fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
    it writes size * count bytes starting from buffer[0].
    Kurt

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    15
    Ok, so do you know any way of writing SIZEMAX copies of 'letra' without having to storage it previously into a variable? Thanx.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by icebabe
    Ok, so do you know any way of writing SIZEMAX copies of 'letra' without having to storage it previously into a variable? Thanx.
    You need to use a loop.
    Kurt

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    15
    Ok, thanks a lot (I was wondering if there was a function that did that but apparently there isn't any). Bye!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 09:48 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21