Thread: Help reading a jpeg file into frames of 100 bytes!! :S

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Help reading a jpeg file into frames of 100 bytes!! :S

    Hey,

    I am trying to do a small part of the assignmet given to me, but cannot manage to get it to work.

    I want to open a jpeg file which already exists.. and then convert it into streams of 100 bytes of code (frame). Upon running the following program, nothing happens?! :S I have no idea what is happening..

    A window pops up saying:

    "An unhandled exception of type 'System.NullReferenceException' occured in 'TryOut.exe.
    Additional Information: Object reference not sent to an instance of an object."

    The following is the code:

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
    	unsigned char frame[100], plz;
    	int i;
    
    	FILE *cfPtr;
    	
    	if((cfPtr = fopen("C:/Documents and Settings/photo1.jpg", "rb") == NULL))
    	{
    		printf("File could not be opened\n");
            scanf("%d\n\n", &plz);
    	}
    
    	else
    	{
    		while (!feof(cfPtr))
    		{
    			for(i = 1; i <= 100; i++)
    			{
                    fread(&frame[i], sizeof(char),1,cfPtr);
    				printf("%d\n", frame[i]);
    				scanf("%d\n\n", &plz);
    			}
    		}
    				fclose(cfPtr);
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by rodiannef View Post

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
    	unsigned char frame[100], plz;
    	int i;
    
    	FILE *cfPtr;
    	
    	if((cfPtr = fopen("C:/Documents and Settings/photo1.jpg", "rb") == NULL))
    	{
    		printf("File could not be opened\n");
                   scanf("&#37;d\n\n", &plz);
    	}
    
    	else
    	{
    		while (!feof(cfPtr))
    		{
    			for(i = 1; i <= 100; i++)
    			{
                                    fread(&frame[i], sizeof(char),1,cfPtr);
    				printf("%d\n", frame[i]);
    				scanf("%d\n\n", &plz);
    			}
    		}
    		fclose(cfPtr);
    	}
    	return 0;
    }
    Your indentation is manic, but anyway, you're over-running the frame[]. C arrays go from 0 to array size - 1, not 1 to array size. Also, you're using %d in printf(), which should be %c, for a char.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    :)

    Yes you have a point! .. I didn't see that one!

    Do you know why it is printing numbers instead of binary from the array frame?

    Thanks for you help

    Code:
    #include <stdio.h>
    
    int main (void){
    
    	unsigned char frame[100];
    	int i;
    
    	FILE *cfPtr;
    	
    	if((cfPtr = fopen("C://Documents and Settings//photo1.jpg", "rb")) == NULL){
    		printf("File could not be opened\n");
    	}
    
    	else{
    		while (!feof(cfPtr)){
    			for(i = 0; i <= 99; i++){
                    fread(&frame[i], sizeof(char),1,cfPtr);  /*reads byte byte*/
    				printf("&#37;c\n", frame[i]);
    			}
    		}
    		fclose(cfPtr);
    	}
    	return 0;
    }
    Last edited by rodiannef; 12-09-2008 at 02:23 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What is binary, if not numbers? printf doesn't have a %thingy for printing in binary. You can do oct or hex if you want.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try this
    Code:
    while ( fread(frame, sizeof(char),sizeof(frame),cfPtr) > 0 ) {
      // for loop here
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  5. Reading Certain number of bytes from a file
    By (TNT) in forum Windows Programming
    Replies: 6
    Last Post: 01-14-2002, 08:35 PM