?
Solved

What end wrong with my program?

Posted on 2003-03-14
7
Medium Priority
?
241 Views
Last Modified: 2010-04-15
Hi,
    I'm trying to read the width, height and maval of pgm images, i have written a portion of the program but i dunno what went wrong can anyone enlighten me?

#include <stdio.h>
#include <stdlib.h>
#include "pgm.h"

int main( PGMImage *img)
{
      int ch;
      FILE *fp;
     // long count = 0;
    // int row, col,
      int type;
     //int MaxVal;
      //int width;
      //int height;
      char string[80];


      printf("Please enter the filename :");
      scanf("%s",&string);
     
     
     if ((fp = fopen(string, "r"))== NULL)
       {
          printf("Can'tOpen %s\n", string);
          exit(1);
     }
        ch = getc(fp);
     if (ch != 'P')
     {
          printf("ERROR(1) : Not Valid PGM File type\n");
          exit(1);
     }
     
     printf("\n The filename is %s\n", string);
     
     ch = getc(fp);
     type = ch - 48;
     if (( type != 2) && ( type != 5))
     {
          printf("EORROR(2): Not Valid PGM File type\n");
     }

     printf("\n The value is %ld\n", type);

     while (getc(fp) != '\n');
     while (getc(fp) == '#')
     {
          while (getc(fp) != '\n');
     }

     fseek(fp, -1, SEEK_CUR);

     fscanf(fp,"%ld", &((*img).width));   //what went here?
     fscanf(fp,"%ld", &((*img).height));
     fscanf(fp,"%ld", &((*img).maxVal));

        printf("\n width  = %d",(*img).width);
     printf("\n height = %d",(*img).height);
     printf("\n maxVal = %d",(*img).maxVal);    
     printf("\n");

     if (((*img).width > Max) || ((*img).height > Max))
     {
          printf("\n\n***ERROR - image too big for current structure ***\n\n");
          exit(1);
     }
     printf("\n You can display the image here\n");

     return 0;
}                                        

my pgm.h file

#ifndef     PGM_H
#define PGM_H

#define Max 1000

/*
#define LOW_VALUE 0
#define HIGH_VALUE 255
*/

struct PGMstructure
{
     int maxVal;
     int width;
     int height;
};
typedef struct PGMstructure PGMImage;
int main(PGMImage *img);
#endif

thanx
0
Comment
Question by:silentkiller700
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8135077
>> my pgm.h file
..
..
>> int main(PGMImage *img);

I think you should remove this line.


Also, in main ():

>> scanf( "%s", &string ) ;

Change it to scanf ( "%s", string ) ;

'string' already refers to the base-address of the array, so an ampersand (&) is not needed.

'Max' is probably not defined.


Mayank.
0
 
LVL 5

Accepted Solution

by:
Kocil earned 60 total points
ID: 8137049
Mayan was right, but there are some more fatal mistake 1. Your PGMImage structure does not contains array to hold the image matrix
2. Your main does not allocate a memory for the img, instead you receive it from the main arguments. BEWARE, you may harm your system with this mistake !

So this is the correction for pgm.h

ifndef PGM_H
#define PGM_H

#define Max 1000

typedef struct {
    int maxVal;
    int width;
    int height;
    unsigned char data[Max][Max]
} PGMImage;

#endif


This is the correction for main()

// Note that main MUST be either one of these
// main(void)
main(argc, char* argv[])
{
   PGMImage* img;
   int ch;
   FILE *fp;
   
   img = (PGMImage*) malloc(sizeof(PGMImage));  

   /** no change for the rest ***/
   ....


   /* at the end */
   free(img);
}

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8137280
Nice going, Kocil. I almost overlooked that there'd be either command-line arugments or a void in main () :-)

Mayank.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:Kocil
ID: 8137659
Second look always sharper :)
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 8141946
i hope there is no need for 'sharpest'=third look
:)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8142820
No, buddy, let's hope not :-))

Mayank.
0
 
LVL 8

Expert Comment

by:a_twixt_in_the_tale
ID: 8172454

sorry but dunno wot a pgm image is

but you could try

1.     // change &string to string; string is an address
     scanf("%s",string);          

2.     // before using img
     // allocate memory for the PGMImage object
     img = malloc(sizeof(PGMImage));

3.     when you're reading in from a file using scanf it expects the numbers to be separated by whitespace. if its like that its ok.
     then using your line
          scanf(fp,"%ld",&(img->width));
     will work
     
     if the numbers are not separated by whitespace, but each are of 4(sizeof(long int) or is it 8) then use fread to read into a character array and then convert that into integer
               char number[5];
               
               number[4]='\0';
               fread(number,4,1,fp);
               img->width = atol(number);
     
     but if the number is actually encoded you will have to process the number array by yourself unless you find some library function for that
     
     
     any links abt pgm image?
     
     thanx
     and good luck
     :-)
     don
     
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question