Buffer Pointer, fread, memset

This is a discussion on Buffer Pointer, fread, memset within the C Programming forums, part of the General Programming Boards category; Need some reassurance Not wondering about syntax or semantics btw. Lets say I open a file and reading from it. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    8

    Buffer Pointer, fread, memset

    Need some reassurance



    Not wondering about syntax or semantics btw.

    Lets say I open a file and reading from it.

    /*reading ten char from file and store it in buffer*/
    char * buffer = malloc(sizeof(char) * 200));
    fread(buffer, 1, 10, file);

    The pointer in buffer always starts at 0 at the beginning

    so by using fread(buffer, 1, 10, file); 20 times I should be able to fill it without overwriting anything right?

    Code may be easier to explain:

    Code:
    char* buffer = malloc(sizeof(char)* 100);
    
    /*set null from buffer[0] to buf[29], 30 bytes*/
    memset(buffer, '\0', 30);
    /*since buffer is pointing at buf[30] now it will start storing from there, therefore storing data from buffer[30] to buffer[59]*/
    fread(buffer, 1, 30, a_file);
    /*will set 0 from buffer[60] to buffer[69]*/
    memset(buffer, '\0', 10);
    
    fread(buffer, 1, 30, a_file);
    /*after this the data from file will be stored in buffer[70] to buffer[99]*/
    What I'm wondering is if this kind of using pointers is possible and will it work.

    Thanks for reply.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    You're overwriting your data, memset() or fread() won't move the value of the "buffer" variable (how could they?), you have to do it by yourself.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    8
    Yeah seems like I have to do that. *(buffer + bytes_used), here I come.

    Thanks, gonna try this tomorrow

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Hoang Bao Ngo
    *(buffer + bytes_used), here I come.
    I hope you write buffer[bytes_used] instead
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    8
    found out that calloc is better than malloc, since I gotta write and pad where there are necessary, so instead of remember where to pad, just pad all at the beginning and then start overwriting data.

    but buffer[bytes_used] didn't work out for me in fread(buffer[bytes_used], 1, filesize, file);
    I'm thinking of using *buffer + bytes_used) to forward the pointer to a place in buffer then start storing data from there. Gonna test it now
    Last edited by Hoang Bao Ngo; 09-05-2012 at 08:12 AM.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Hoang Bao Ngo View Post
    but buffer[bytes_used] didn't work out for me in fread(buffer[bytes_used], 1, filesize, file);
    buffer[bytes_used] is a char (the char at position bytes_used in your buffer array)
    fread() expects a pointer to the array as its first argument, thus you have to use &buffer[bytes_used], which is the address of element buffer[bytes_used].

    I'm thinking of using *buffer + bytes_used) to forward the pointer to a place in buffer then start storing data from there. Gonna test it now
    That won't work either.
    *buffer + bytes_used will add the value of bytes_used to the first char in your buffer array (buffer[0])
    *(buffer + bytes_used) will be the value of the element buffer[bytes_used] (see above)
    buffer + bytes_used is equivalent to &buffer[bytes_used].

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    8
    I understand the error now thank you, it expected a pointer but I gave it a char, or what was in that address.

    I want to set the next place to a null so I use:
    *(boot_buffer+bytes_used++) = '\0';
    without * I get a compile error.
    so if boot_buffer is already malloc'ed with sizeof(char) * 500.
    and I want to set all of 500 bytes to null( I know memset and bzero and calloc is more useful here, but just an example to know for sure how pointers work).
    so
    Code:
    int count = 0;
    while(count < 500){
      *(boot_buffer+bytes_used++) = '\0';
    }

    If I understood it right, then this work, since I just changed what was in the *(buffer + bytes_used++) to another value and forwarded the pointer at the same time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-27-2012, 07:05 AM
  2. Replies: 7
    Last Post: 04-27-2009, 05:56 PM
  3. fread & fseek not advancing buffer
    By RobotGymnast in forum C++ Programming
    Replies: 6
    Last Post: 10-28-2008, 04:38 PM
  4. Obtaining a pointer to the video buffer
    By cboard_member in forum Game Programming
    Replies: 7
    Last Post: 04-04-2006, 11:00 PM
  5. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 06:57 PM

Tags for this Thread


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