Thread: Simple reading in a file help needed.

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    5

    Simple reading in a file help needed.

    So im just trying to read in an pgm file and store it in an array,

    Code:
    int** imageArray;
    
    
    //allocate the image array
    	imageArray = (int**) malloc(row * sizeof(int*));
    	for(i = 0; i < row; i++)
    		imageArray[i] = (int*) malloc(col * sizeof(int));
    		
    	for(i=0; i < row; i++)
    	{
    
    
    		for(j=0; j < col; j++)
    		{
    			fscanf(fpI, "%d ", &imageArray[i][j]);
    		}
    	}
    It stores about 1/3 of the file into the array, and then starts printing 6815940 6819608 followed by a bunch of 0's.
    Thanks for any help in advance.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You have problems printing the array elements so what's the code for it?
    Also show us the header of the pgm file.

    BTW: Don't cast the return value of malloc in C.

    Bye, Andreas

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    And what is the format of the image file?
    Post a couple of rows.

    Code:
        for(i=0; i < row && !feof(fpl); i++)
        {
            for(j=0; j < col && !feof(fpl); j++)
            {
                if ( fscanf(fpI, "%d ", &imageArray[i][j]) != 1 ) {
                    fprintf(stderr,"Problem at row %d col %d\n", i, j );
                }
            }
        }
    Also, add some diagnostic and error checking to your code.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    the pgm files format is as follows
    Code:
    P2
    # Image generated by Artifex Ghostscript (device=pgm)
    448 336
    255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
    and my printing to the file is as follows
    Code:
    //writing to the new file
    		//header
    	fprintf(fpO, "%s%s%d %d\n%s", InFileType, InFileDescription, row, col, maxValueIn);
    		//Binary threshold
    		if(Uinput == 1)
    		{
    		for(i = 0; i < row; i++)
    		{
    			for(j = 0; j < col; j++)
    			{
    				fprintf(fpO,"%d ", imageArray[i][j]);
    			}
    			fprintf(fpO,"\n");
    		}
    		}//end Binary threshold
    thanks for any input.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    All I can suggest is add debug prints (or use a debugger) and step through the code.
    There isn't anything obviously wrong with what you've posted so far.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by Salem View Post
    All I can suggest is add debug prints (or use a debugger) and step through the code.
    There isn't anything obviously wrong with what you've posted so far.
    do you have any suggestions for debuggers? and would using fgets to get all the header info effect my fscanf i use to declare the array?

    Code:
    //declare variables for image file intake
    	int** imageArray;
    	char InFileType [10];
    	char InFileDescription [100];
    	char rowRead;
    	char inDimensions [10];
    	char maxValueIn [10];
    	char fileDescription [100];
    	unsigned int row, col, i, j, maxValue;
    
    
    	//reading in the information
    	fgets(InFileType, 10, fpI);
    	fgets(InFileDescription, 100, fpI);
    	fgets(inDimensions, 10, fpI);
    	row = atoi(strtok(inDimensions, " "));
    	col = atoi(strtok(NULL, " "));
    	fgets(maxValueIn, 10, fpI);
    	maxValue = atoi(maxValueIn);

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Quote Originally Posted by Denis St. John View Post
    do you have any suggestions for debuggers? and would using fgets to get all the header info effect my fscanf i use to declare the array?

    Code:
    //declare variables for image file intake
        int** imageArray;
        char InFileType [10];
        char InFileDescription [100];
        char rowRead;
        char inDimensions [10];
        char maxValueIn [10];
        char fileDescription [100];
        unsigned int row, col, i, j, maxValue;
    
    
        //reading in the information
        fgets(InFileType, 10, fpI);
        fgets(InFileDescription, 100, fpI);
        fgets(inDimensions, 10, fpI);
        row = atoi(strtok(inDimensions, " "));
        col = atoi(strtok(NULL, " "));
        fgets(maxValueIn, 10, fpI);
        maxValue = atoi(maxValueIn);
    I ran a debugg on the Fscanf, and its returning 0 when the array is at i = 150 and j =297 when rows = 448 and col = 336.

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    5
    Idk if anyone still have some info, but im trying to eliminate fscanf.
    Code:
    	//allocate the image array
    	imageArray = malloc(row * sizeof(int*));
    	for(i = 0; i < row; i++)
    		imageArray[i] = malloc(col * sizeof(int));
    
    
    	for(i=0; i < row; i++)
    	{
    		for(j=0; j < (col/16); j++)
    		{
    			fgets(rowRead, 10000, fpI);
    			for(k=0; k< 16; k++)
    			{
    				sscanf(rowRead, "%d", &imageArray[i][(j*16)+k]);
    			}
    		}
    	}
    it's storing the image kind of blocky. any thoughts?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Is your image data int sized? Unsigned char is what I've seen in some image formats.

    When you look at the data with a hex editor, can you get a better description than "blocky", of the problem?

    There are freeware hex editors at the file depot sites, for Windows.
    Last edited by Adak; 02-28-2013 at 09:08 PM.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    At this point, I would suggest post a whole program.

    > it's storing the image kind of blocky. any thoughts?
    Because sscanf doesn't work it's way through a string in the same way that fscanf works it's way through a file stream.

    But since you decided to ignore previous suggestions like adding lots of error checking, but instead go off and try some other half-assed approach, I'll just leave it at that.
    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.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    505
    Try creating a few trivial images, eg 1 pixel, 7 x 8, and so on.

    |Most likely the problem is that you haven't got row and col correct, or the image itself is defective.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading text file to struct help needed!
    By werdy666 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 11:37 AM
  2. Help is needed, reading a string
    By yuzhangoscar in forum C Programming
    Replies: 2
    Last Post: 09-21-2008, 09:26 PM
  3. simple file reading
    By l2u in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2008, 07:10 AM
  4. reading from file to an array - help needed!
    By timeforheroes in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 12:16 PM
  5. Simple AI Needed!
    By minime6696 in forum Game Programming
    Replies: 2
    Last Post: 02-15-2002, 09:38 PM