strlen(buffer) on binary data

This is a discussion on strlen(buffer) on binary data within the C Programming forums, part of the General Programming Boards category; I wanted to write a function like this that would return a buffer containing a binary file: Code: char *getbin ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    strlen(buffer) on binary data

    I wanted to write a function like this that would return a buffer containing a binary file:

    Code:
    char *getbin (char *filename) {
    	char *buffer;
    	struct stat finfo;
    	FILE *fstRO = fopen(filename, "rb");
    	if (fstRO == NULL) return NULL;
    	stat(filename,&finfo);
    	buffer=ec_malloc(finfo.st_size);
    	if (fread(buffer,1,finfo.st_size,fstRO) != finfo.st_size) {
    		fclose(fstRO); return NULL;}
    	fclose(fstRO); return buffer;
    }
    It works fine -- the only problem is it's impossible to determine how big the returned buffer is! strlen() returns the wrong number (hitting a 0, i guess). Do I have to change the function to return a typedef struct containing the buffer and the size, or is there some way to measure a buffer containing binary data?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    One way would be to "return" the size as well. This can be done by passing int *size or something like that into the function.

    That said, I'm not sure I'm in love with passing allocated memory out of this thing like you are doing.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    No you will have to pass a pointer as an additional parameter or else 'piggyback' it onto the buffer itself (I'd prefer the former, personally).
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by FillYourBrain View Post
    One way would be to "return" the size as well. This can be done by passing int *size or something like that into the function.
    I like that suggestion, thanks.

    Quote Originally Posted by FillYourBrain View Post
    That said, I'm not sure I'm in love with passing allocated memory out of this thing like you are doing.
    Isn't that normal as long as I free it later? Is it better to pass a pointer to the function for allocation (why)?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    yeah that part is what I'm not sure about... It seems you just want a quick single call for loading the file. Fine, but I would usually prefer to get the size of the file first and pass the buffer into a call like this. But then I'm weird like that.

    I don't tell people that their approach is wrong though, and I'm not saying that here.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,594
    Incidentally, shouldn't you free the buffer if fread() returns something other than finfo.st_size? At the moment it seems to me that there is a memory leak there since only NULL is returned in that case, thus there is no way to free the buffer.

    EDIT:
    Oh, and are you sure you do not need to check if ec_malloc() returns a null pointer? The standard malloc() returns a null pointer if memory could not be allocated.
    Last edited by laserlight; 10-07-2008 at 08:22 AM.
    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

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Incidentally, shouldn't you free the buffer if fread() returns something other than finfo.st_size?
    Yep. Thanks for noticing. And I should check buffer != NULL after the malloc...
    Last edited by MK27; 10-07-2008 at 08:52 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. store binary data in program...
    By Abda92 in forum C Programming
    Replies: 9
    Last Post: 03-23-2008, 10:33 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  4. Binary comparison
    By tao in forum Windows Programming
    Replies: 0
    Last Post: 06-28-2006, 12:10 PM
  5. Replies: 4
    Last Post: 06-14-2005, 05:45 AM

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