Like Tree3Likes
  • 1 Post By anduril462
  • 1 Post By R.Stiltskin
  • 1 Post By nonoob

C fwrite with JPEG Data

This is a discussion on C fwrite with JPEG Data within the C Programming forums, part of the General Programming Boards category; Hi All, I'm very new to C programming.....I usually stick to PHP and AS3. But I'm trying to write a ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    C fwrite with JPEG Data

    Hi All,

    I'm very new to C programming.....I usually stick to PHP and AS3.

    But I'm trying to write a PHP extension in C to handle JPEG data that comes in via AS3 (a flash application I built) and write it to a file.

    In PHP it looks like this:
    PHP Code:
    $handle=fopen($file"w"); 
    fwrite($handle$data); 
    fclose($handle); 
    It simply takes already JPEG-Encoded data and puts it in a JPEG file. But I can't get it to do the same in C. I have this:

    Code:
    zval *prefile; // file name
    	zval *predatas; // jpeg data
    	long flen, clen, datas_len;
    	size_t len = 0;
    	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|z", &prefile, &predatas, &flen, &clen, &datas_len) == FAILURE) {
    		return;
    	}
        char *file = Z_STRVAL_P(prefile); // file name
        char *data = Z_STRVAL_P(predatas); // jpeg data
    	
    	FILE *fp = fopen(file,"w"); // open the file
    	len = strlen(data); // length of data
        fwrite(data, len, 1, fp); // fwrite the data
        if(fclose(fp)) { // close the data
    	RETURN_STRING("true", 1);
    	} else {
    	RETURN_STRING("false", 1);
    	}
    Any ideas on what I may be doing wrong? Keep in mind the data I get from flash player is already JPEG encoded data.....it just needs a file to live in which is what I want C to do.

    Any suggestions? Thanks,
    Alex

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,456
    How exactly is it not working? We need some specifics, like error messages. Can you even compile the code? Does it write the file, but incorrectly or not write the file at all. You always need to check the return value of your file functions and print a useful error message if they fail. Look into perror(), the errno variable and strerror(). fopen will return NULL on failure, and fwrite should return the number of bytes written. Make sure they did what you expected.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,456
    Also, note that JPEG data will most likely contain zero bytes, which is the '\0' character. That signifies the end of a string in C, and is what strlen uses to determine end of string. You are probably getting an incorrect length for fwrite and will need a better way to get the true length of the data to write.
    Salem likes this.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You're treating your jpeg data as if it were a string -- but it's not. A string is a null-terminated char array. When you use strlen(data) to pass the size of the array to fwrite, you are measuring the length in bytes from the beginning of the array up to the first 0 -- which probably doesn't even get you past the header.
    Salem likes this.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I agree. strlen() is not appropriate to use here. Plus the file should be opened using "wb".
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Sorry about that! I don't know what I was thinking when I didn't put my problem in the message. My bad.

    It does create a file, which is named properly, but it's only 4 bytes each time. When I change the file extension to .txt, I can see four odd characters in the file like this: ˇˇ‡

    So yes, I'm able to compile the code without problems.

    Now, what's interesting is that when I tell it to put text in the file instead of JPEG data, it writes the text properly. So I can create a file, for example, called test.txt with "hello world" in it. So it's just a problem with the JPEG data. Maybe it has something to do with the way I bring the variables in or something, but I'm not experienced enough with C to know what to look for in the code. I've spent most of the day today working on this.

    Thanks!

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Quote Originally Posted by R.Stiltskin View Post
    You're treating your jpeg data as if it were a string -- but it's not. A string is a null-terminated char array. When you use strlen(data) to pass the size of the array to fwrite, you are measuring the length in bytes from the beginning of the array up to the first 0 -- which probably doesn't even get you past the header.
    Yeah, I figured it might have something to do with that.....is there another way to do it that I should try?

    Thanks for all of the quick replies everybody.....this is really helping me out.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,456
    Whatever you pass to this PHP extension must know the data and length. Make sure both of those make it down to your C function, and when you call fwrite, use that length you pass in.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    What data type should my JPEG data be in? Long?

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Did you get prototype for this function Z_STRVAL_P(predatas) ? Or maybe zval? If pointer is appropriate then it really doesn't matter whether it's char *, unsigned char *, or any other. It's just an address pointing to some contiguous block of memory. As was mentioned before, there should be a way for you to find out what the length is. Is it in units of bytes? shorts? longs?

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Ahh, got it! It was just about getting that third parameter through there for the size.

    One additional question however, if I want to combine a variable with some string text, what's the best way?

    For example, in PHP, I'd do this:
    PHP Code:
    $filename "1234";
    $file =  $filename ".jpg";

    echo 
    $file//this would give me "1234.jpg"; 
    Thanks!

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    The usual way in C is to use sprintf which works basically the same as printf except it writes to a string instead of to the console.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. jpeg data in mbox files
    By MK27 in forum Tech Board
    Replies: 3
    Last Post: 07-30-2009, 12:56 PM
  2. Replies: 2
    Last Post: 12-26-2008, 03:38 PM
  3. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  4. JPEG, GIF etc.
    By playwin in forum Windows Programming
    Replies: 6
    Last Post: 01-19-2005, 11:05 AM
  5. Jpeg , Jpg
    By Sunny in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 04-10-2002, 12:26 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