Thread: getch() prob

  1. #16
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    getting this error.
    Code:
     49: Cannot convert 'void *' to 'int * * *' in functi
    on main()
     129: Cannot convert 'unsigned char' to 'int *' in fu
    nction main()
    *** 2 errors in Compile ***
    Code:
    /* greypic.cpp */
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    #include <malloc.h>
    
    /*-------STRUCTURES---------*/
    typedef struct sImage{int rows; int cols; int bits; unsigned char* data;} sImage ;
    
    /*-------PROTOTYPES---------*/
    long getImageInfo(FILE*, long, int);
    void copyImageInfo(FILE*, FILE*);
    void copyColorTable(FILE*, FILE*, int);
    
    int main()
    {
    
    
    
    getch();
    
    
    
      FILE         *bmpInput;
      FILE         *bmpOutput;
      sImage        originalImage;
      unsigned char  someChar;
      unsigned char *pChar;
      int         nColors;  /* BMP number of colors */
      long         fileSize; /* BMP file size */
      int         vectorSize; /* BMP vector size */
      int         r;
      int         c;       /* r = rows, c = cols */
    /*   int         pixels[600][600]; */
    
    /* dynamic memory allocation of pixels[][] */
    
    /*  int * pixels; */
    
    
    
      int *** pixels;
      pixels = malloc(600*600*sizeof(***pixels)); //no cast needed in C see faq
    if(!pixels)
    {
     printf("Error allocating memory\n");
      exit(1);
    }
    
      int i;
    
      char imagein[30];
      char imageout[30];
    
      /* initialize pointer */
      someChar = '0';
      pChar = &someChar;
      printf("Enter input image : ");
      fgets(imagein , sizeof(imagein) , bmpInput) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imagein) ; i++ )
      {
        if ( imagein[i] == '\n' )
        {
            imagein[i] = '\0';
            break;
        }
      }
     
      printf("Enter output image : ");
      fgets(imageout , sizeof(imageout) , bmpOutput) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imageout) ; i++ )
      {
        if ( imageout[i] == '\n' )
        {
            imageout[i] = '\0';
            break;
        }
      }
      
     
      /*--------READ INPUT FILE------------*/
      bmpInput = fopen(imagein, "rb");
      fseek(bmpInput, 0L, SEEK_END);
     
      /*--------DECLARE OUTPUT FILE--------*/
      bmpOutput = fopen(imageout, "wb");
    
      /*--------GET BMP DATA---------------*/
      originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
      originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
      originalImage.bits = (int)getImageInfo(bmpInput, 28, 4);
      fileSize = getImageInfo(bmpInput, 2, 4);
      nColors = getImageInfo(bmpInput, 46, 4);
      vectorSize = fileSize - (14 + 40 + 4*nColors);
    
      /*-------PRINT DATA TO SCREEN-------------*/
      printf("Width: %d\n", originalImage.cols);
      printf("Height: %d\n", originalImage.rows);
      printf("Bits per Pixel: %d\n", originalImage.bits);
      printf("File size: %ld\n", fileSize);
      printf("# Colors: %d\n", nColors);
      printf("Vector size: %d\n", vectorSize);
     
      copyImageInfo(bmpInput, bmpOutput);
      copyColorTable(bmpInput, bmpOutput, nColors);
    
      /*----START AT BEGINNING OF RASTER DATA-----*/
      fseek(bmpInput, (54 + 4*nColors), SEEK_SET);
    
      /*----------READ RASTER DATA----------*/
      for(r=0; r< originalImage.rows ; r++)
      {
        for(c=0; c< originalImage.cols ; c++)
        {
          /*-----read data, reflect and write to output file----*/
          fread(pChar, sizeof(char), 1, bmpInput);
          pixels[r][c] = *pChar;
          *pChar = 255 - *pChar;
          fwrite(pChar, sizeof(char), 1, bmpOutput);
        }
      }
     
      fclose(bmpInput);
      fclose(bmpOutput);
     
    
        getch();
    
     return 0;
    
    
    }
    
    /*----------GET IMAGE INFO SUBPROGRAM--------------*/
    long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
    {
      unsigned char      *ptrC;
      long         value = 0L;
      unsigned char      dummy;
      int         i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, offset, SEEK_SET);
    
      for(i=1; i< numberOfChars; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        /* calculate value based on adding bytes */
        value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
      return(value);
    
    } /* end of getImageInfo */
    
    /*-------------COPIES HEADER AND INFO HEADER----------------*/
    void copyImageInfo(FILE* inputFile, FILE* outputFile)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 0L, SEEK_SET);
      fseek(outputFile, 0L, SEEK_SET);
    
      for(i=0; i<=50; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    }
    
    /*----------------COPIES COLOR TABLE-----------------------------*/
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 54L, SEEK_SET);
      fseek(outputFile, 54L, SEEK_SET);
    
      for(i=0; i<(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    ok yeah then cast it to int ** are you compiling using a C++ compiler or C?

    you're using C code but you have a cpp extesion why?
    Last edited by robwhit; 07-04-2007 at 11:41 AM. Reason: indirection problem fixed.

  3. #18
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i do not get how some can compile the prog and it works fine. see the other thread. and i have to fix alot of probs? even sometimes when i do it still does not work. even if the code looks ok. var[a][a] should have worked. do not know why it did not work here. or why it prevents getch from waiting.

    [meowedit] borland c++ free command line tool

    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland [/meowedit]
    Last edited by kryptkat; 07-03-2007 at 01:07 PM.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    if the variable is too big for the stack, then it won't fit in the small space that it has available for that. So the program might not even get loaded correctly or something, or the OS might read it wrong (unlikely though). I'm surprised you didn't get an error though.

    these things happen sometimes, it's just the way programming sometimes is.

    A way to limit this is to test frequently, that way you know know where the problem is likely to be.

    edit: rename the file to .c if you're doing C (looks that way)

    edit: or, maybe it doesn't make a valid program file. (the stack thing)
    Last edited by robwhit; 07-03-2007 at 09:42 PM.

  5. #20
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    i did try to compile it as c a long time ago and got many errors. prog looks ok now so i blame the compiler. looked like a nice little prog too. another prog for the back que one day i will start over rather than from an example prog. well i did learn about new and delete as dynamic memory allocation. used malloc many
    times with no prob. thank you all again.

    end thread.

  6. #21
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so i blame the compiler
    Don't do this...

    Having
    getch () on the first line of the main - you have not a correct C-code. In c you cannot mix declaration and other statements...

    so blame your knowledge of C
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #22
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > int pixels[600][600];
    Move this line above main(), and it will work with the borland compiler. This array is too big for the stack.
    Or, if you prefer to allocate the array dynamically:
    Code:
    	int **pixels;
    	int i, j;
    
    	pixels = malloc(600 * sizeof *pixels);
    
    	for (i=0; i<600; i++)
    	{
    		pixels[i] = malloc(600 * sizeof *pixels[i]);
    	}
    And you should free the memory at the end of the program:
    Code:
    	for (i=0; i<600; i++)
    	{
    		free(pixels[i]);
    	}
    	free(pixels);
    The advantage being instead of using magic numbers like 600x600, you can use the height and width of the image when you call malloc().

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Also note that in this case (as Salem pointed out) the array is never actually used for anything in the original code, so you could comment references to it.

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    some of my code was wrong. It's fixed now.

  10. #25
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    when dynamically allocate memory
    Code:
    Error E2034 greypic.cpp 64: Cannot convert 'void *' to 'int * *' in function
     main()
    Error E2034 greypic.cpp 68: Cannot convert 'void *' to 'int *' in function m
    ain()
    changed protortype to in got more errors then changed back to void type.

    here are those 6 errors
    Code:
    Error E2034 greypic.cpp 64: Cannot convert 'void *' to 'int * *' in function
     main()
    Error E2034 greypic.cpp 68: Cannot convert 'void *' to 'int *' in function m
    ain()
    Error E2356 greypic.cpp 192: Type mismatch in redeclaration of 'copyImageInf
    o(FILE *,FILE *)'
    Error E2344 greypic.cpp 16: Earlier declaration of 'copyImageInfo(FILE *,FIL
    E *)'
    Warning W8070 greypic.cpp 207: Function should return a value in function co
    pyImageInfo(FILE *,FILE *)
    Error E2356 greypic.cpp 211: Type mismatch in redeclaration of 'copyColorTab
    le(FILE *,FILE *,int)'
    Error E2344 greypic.cpp 17: Earlier declaration of 'copyColorTable(FILE *,FI
    LE *,int)'
    Warning W8070 greypic.cpp 228: Function should return a value in function co
    pyColorTable(FILE *,FILE *,int)
    *** 6 errors in Compile ***
    then put pixels[600][600] above main as suggested and it compiled but did not run correctly

    here is the output. also got windows popup error report "must shut down program"
    Code:
    C:\borland\bcc55\bin>bcc32 greypic.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    greypic.cpp:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    
    C:\borland\bcc55\bin>greypic
    Enter input image :
    C:\borland\bcc55\bin>greypic
    Enter input image :
    C:\borland\bcc55\bin>
    this is the closest it has come to working. so i will blame the compiler. i think the compiler
    has a case of pms <program mutalation syndrom>

    Code:
    /* greypic.cpp */
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    #include <malloc.h>
    
    /*-------STRUCTURES---------*/
    typedef struct sImage{int rows; int cols; int bits; unsigned char* data;} sImage ;
    
    /*-------PROTOTYPES---------*/
    long getImageInfo(FILE*, long, int);
    void copyImageInfo(FILE*, FILE*);
    void copyColorTable(FILE*, FILE*, int);
    
    int         pixels[600][600];
    
    int main()
    {
    
    
    
    getch();
    
    
    
      FILE         *bmpInput;
      FILE         *bmpOutput;
      sImage        originalImage;
      unsigned char  someChar;
      unsigned char *pChar;
      int         nColors;  /* BMP number of colors */
      long         fileSize; /* BMP file size */
      int         vectorSize; /* BMP vector size */
      int         r;
      int         c;       /* r = rows, c = cols */
    
    
    /*   note pixels[600][600] is never used in orginal code */
    /*   int         pixels[600][600]; */
    
    
      int i; 
    
      char imagein[30];
      char imageout[30];
    
      /* initialize pointer */
      someChar = '0';
      pChar = &someChar;
      printf("Enter input image : ");
      fgets(imagein , sizeof(imagein) , bmpInput) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imagein) ; i++ )
      {
        if ( imagein[i] == '\n' )
        {
            imagein[i] = '\0';
            break;
        }
      }
     
      printf("Enter output image : ");
      fgets(imageout , sizeof(imageout) , bmpOutput) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imageout) ; i++ )
      {
        if ( imageout[i] == '\n' )
        {
            imageout[i] = '\0';
            break;
        }
      }
      
     
      /*--------READ INPUT FILE------------*/
      bmpInput = fopen(imagein, "rb");
      fseek(bmpInput, 0L, SEEK_END);
     
      /*--------DECLARE OUTPUT FILE--------*/
      bmpOutput = fopen(imageout, "wb");
    
      /*--------GET BMP DATA---------------*/
      originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
      originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
      originalImage.bits = (int)getImageInfo(bmpInput, 28, 4);
      fileSize = getImageInfo(bmpInput, 2, 4);
      nColors = getImageInfo(bmpInput, 46, 4);
      vectorSize = fileSize - (14 + 40 + 4*nColors);
    
      /*-------PRINT DATA TO SCREEN-------------*/
      printf("Width: %d\n", originalImage.cols);
      printf("Height: %d\n", originalImage.rows);
      printf("Bits per Pixel: %d\n", originalImage.bits);
      printf("File size: %ld\n", fileSize);
      printf("# Colors: %d\n", nColors);
      printf("Vector size: %d\n", vectorSize);
     
      copyImageInfo(bmpInput, bmpOutput);
      copyColorTable(bmpInput, bmpOutput, nColors);
    
      /*----START AT BEGINNING OF RASTER DATA-----*/
      fseek(bmpInput, (54 + 4*nColors), SEEK_SET);
    
      /*----------READ RASTER DATA----------*/
      for(r=0; r< originalImage.rows ; r++)
      {
        for(c=0; c< originalImage.cols ; c++)
        {
          /*-----read data, reflect and write to output file----*/
          fread(pChar, sizeof(char), 1, bmpInput);
          pixels[r][c] = *pChar;
          *pChar = 255 - *pChar;
          fwrite(pChar, sizeof(char), 1, bmpOutput);
        }
      }
     
      fclose(bmpInput);
      fclose(bmpOutput);
     
    
        getch();
    
    	for (i=0; i<600; i++)
    	{
    		free(pixels[i]);
    	}
    	free(pixels);
    
     return 0;
    
    
    }
    
    /*----------GET IMAGE INFO SUBPROGRAM--------------*/
    long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
    {
      unsigned char      *ptrC;
      long         value = 0L;
      unsigned char      dummy;
      int         i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, offset, SEEK_SET);
    
      for(i=1; i< numberOfChars; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        /* calculate value based on adding bytes */
        value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
      return(value);
    
    } /* end of getImageInfo */
    
    /*-------------COPIES HEADER AND INFO HEADER----------------*/
    void copyImageInfo(FILE* inputFile, FILE* outputFile)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 0L, SEEK_SET);
      fseek(outputFile, 0L, SEEK_SET);
    
      for(i=0; i<=50; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    }
    
    /*----------------COPIES COLOR TABLE-----------------------------*/
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 54L, SEEK_SET);
      fseek(outputFile, 54L, SEEK_SET);
    
      for(i=0; i<(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }

  11. #26
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    /* greypic.c */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    #include <malloc.h>
    
    /*-------STRUCTURES---------*/
    typedef struct sImage{int rows; int cols; int bits; unsigned char* data;} sImage ;
    
    /*-------PROTOTYPES---------*/
    long getImageInfo(FILE*, long, int);
    void copyImageInfo(FILE*, FILE*);
    void copyColorTable(FILE*, FILE*, int);
    
    
    int main()
    {
      FILE         *bmpInput;
      FILE         *bmpOutput;
      sImage        originalImage;
      unsigned char  someChar;
      unsigned char *pChar;
      int         nColors;  /* BMP number of colors */
      long         fileSize; /* BMP file size */
      int         vectorSize; /* BMP vector size */
      int         r, c;       /* r = rows, c = cols */
      int i;
      int ** pixels;
    
      char imagein[30];
      char imageout[30];
    
      /* initialize pointer */
      someChar = '0';
      pChar = &someChar;
      printf("Enter input image : ");
      fgets(imagein , sizeof(imagein) , stdin) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imagein) ; i++ )
      {
        if ( imagein[i] == '\n' )
        {
            imagein[i] = '\0';
            break;
        }
      }
     
      printf("Enter output image : ");
      fgets(imageout , sizeof(imageout) , stdin) ;
    
    /* slash n strip added */
    
      for(i = 0; i < sizeof(imageout) ; i++ )
      {
        if ( imageout[i] == '\n' )
        {
            imageout[i] = '\0';
            break;
        }
      }
      
     
      /*--------READ INPUT FILE------------*/
      bmpInput = fopen(imagein, "rb");
    
      /* check for fopen failure */
      if(bmpInput == NULL)
      {
        printf("Error opening file\n");
        return 1;
      }
    
      fseek(bmpInput, 0L, SEEK_END);
     
      /*--------DECLARE OUTPUT FILE--------*/
      bmpOutput = fopen(imageout, "wb");
    
      if (bmpOutput == NULL)
      {
        printf("Error opening file\n");
        return 1;
      }
    
      pixels = (int**)malloc(600*600*sizeof(**pixels));
      if(!pixels)
      {
        printf("Error allocating memory\n");
        exit(1);
      }
    
    
    
      /*--------GET BMP DATA---------------*/
      originalImage.cols = (int)getImageInfo(bmpInput, 18, 4);
      originalImage.rows = (int)getImageInfo(bmpInput, 22, 4);
      originalImage.bits = (int)getImageInfo(bmpInput, 28, 4);
      fileSize = getImageInfo(bmpInput, 2, 4);
      nColors = getImageInfo(bmpInput, 46, 4);
      vectorSize = fileSize - (14 + 40 + 4*nColors);
    
      /*-------PRINT DATA TO SCREEN-------------*/
      printf("Width: &#37;d\n", originalImage.cols);
      printf("Height: %d\n", originalImage.rows);
      printf("Bits per Pixel: %d\n", originalImage.bits);
      printf("File size: %ld\n", fileSize);
      printf("# Colors: %d\n", nColors);
      printf("Vector size: %d\n", vectorSize);
     
      copyImageInfo(bmpInput, bmpOutput);
      copyColorTable(bmpInput, bmpOutput, nColors);
    
      /*----START AT BEGINNING OF RASTER DATA-----*/
      fseek(bmpInput, (54 + 4*nColors), SEEK_SET);
    
      /*----------READ RASTER DATA----------*/
      for(r=0; r< originalImage.rows ; r++)
      {
        for(c=0; c< originalImage.cols ; c++)
        {
          /*-----read data, reflect and write to output file----*/
          fread(pChar, sizeof(char), 1, bmpInput);
          pixels[r][c] = *pChar;
          *pChar = 255 - *pChar;
          fwrite(pChar, sizeof(char), 1, bmpOutput);
        }
      }
     
      fclose(bmpInput);
      fclose(bmpOutput);
     
    
      getch();
    
      free(pixels);
    
      return 0;
    
    
    }
    
    /*----------GET IMAGE INFO SUBPROGRAM--------------*/
    long getImageInfo(FILE* inputFile, long offset, int numberOfChars)
    {
      unsigned char      *ptrC;
      long         value = 0L;
      unsigned char      dummy;
      int         i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, offset, SEEK_SET);
    
      for(i=1; i< numberOfChars; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        /* calculate value based on adding bytes */
        value = (long)(value + (*ptrC)*(pow(256, (i-1))));
      }
      return(value);
    
    } /* end of getImageInfo */
    
    /*-------------COPIES HEADER AND INFO HEADER----------------*/
    void copyImageInfo(FILE* inputFile, FILE* outputFile)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 0L, SEEK_SET);
      fseek(outputFile, 0L, SEEK_SET);
    
      for(i=0; i<=50; i++)
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    }
    
    /*----------------COPIES COLOR TABLE-----------------------------*/
    void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors)
    {
      unsigned char      *ptrC;
      unsigned char      dummy;
      int i;
    
      dummy = '0';
      ptrC = &dummy;
    
      fseek(inputFile, 54L, SEEK_SET);
      fseek(outputFile, 54L, SEEK_SET);
    
      for(i=0; i<(4*nColors); i++)  /* there are (4*nColors) bytesin color table */
      {
        fread(ptrC, sizeof(char), 1, inputFile);
        fwrite(ptrC, sizeof(char), 1, outputFile);
      }
    
    }
    http://www.cplusplus.com/reference/c...dio/fopen.html
    Last edited by robwhit; 07-04-2007 at 09:09 AM.

  12. #27
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    am not pur pur purfect? that is hard to swallow.... I need refresher review. Going back and rereading faq and tuts. And books.... still having difficulties with learning c++ think c review best first. Thank you for showing me correct code. Few q.

    Code:
    C:\borland\bcc55\bin>bcc32 fixedgreypic.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    fixedgreypic.cpp:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    
    C:\borland\bcc55\bin>fixedgreypic
    Enter input image : pictestgrey.bmp
    Enter output image : pictestoutgrey.bmp
    Width: 256
    Height: 256
    Bits per Pixel: 8
    File size: 66614
    # Colors: 0
    Vector size: 66560
    
    C:\borland\bcc55\bin>
    
    
    
    06/29/2007  08:18 PM            66,614 lena256.bmp
    06/29/2007  08:45 PM            66,614 pictestgrey.bmp
    07/04/2007  05:17 PM                51 pictestoutgrey.bmp
    Code:
      printf("Enter output image : ");
      fgets(imageout , sizeof(imageout) , stdin) ;
    should that be stdout?

    Code:
      /* check for fopen failure */
      if(bmpInput == NULL)
      {
        printf("Error opening file\n");
        return 1;
      }
    why return 1 and not return -1?

    Code:
      pixels = (int**)malloc(600*600*sizeof(**pixels));
      if(!pixels)
      {
        printf("Error allocating memory\n");
        exit(1);
      }
    why exit(1) instead of exit(0)?

  13. #28
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by kryptkat View Post
    am not pur pur purfect? that is hard to swallow.... I need refresher review. Going back and rereading faq and tuts. And books.... still having difficulties with learning c++ think c review best first. Thank you for showing me correct code. Few q.

    Code:
    C:\borland\bcc55\bin>bcc32 fixedgreypic.cpp
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    fixedgreypic.cpp:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    
    C:\borland\bcc55\bin>fixedgreypic
    Enter input image : pictestgrey.bmp
    Enter output image : pictestoutgrey.bmp
    Width: 256
    Height: 256
    Bits per Pixel: 8
    File size: 66614
    # Colors: 0
    Vector size: 66560
    
    C:\borland\bcc55\bin>
    
    
    
    06/29/2007  08:18 PM            66,614 lena256.bmp
    06/29/2007  08:45 PM            66,614 pictestgrey.bmp
    07/04/2007  05:17 PM                51 pictestoutgrey.bmp
    Code:
      printf("Enter output image : ");
      fgets(imageout , sizeof(imageout) , stdin) ;
    should that be stdout?
    Nope, you're reading from stdin. fgets() gets a string. You don't read from stdout, you print to it. In fact, printf(...) is the same as fprintf(stdout, ...).

    Code:
      /* check for fopen failure */
      if(bmpInput == NULL)
      {
        printf("Error opening file\n");
        return 1;
      }
    why return 1 and not return -1?
    The typical value to return from main upon error is 1. That's just the way it is. (EXIT_FAILURE from <stdlib.h> is the standard, but it's usually 1.) Of course, you can return whatever value you want. Anything but zero is taken to be an error state. Many programs return different error values depending on what went wrong.

    Code:
      pixels = (int**)malloc(600*600*sizeof(**pixels));
      if(!pixels)
      {
        printf("Error allocating memory\n");
        exit(1);
      }
    why exit(1) instead of exit(0)?
    Same. 1 is an error value. exit(0) would be like returning zero, indicating to any programs that examine this program's return value that nothing went wrong, which was not in fact the case.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #29
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    why did that only copy the header <50bytes> and not the entire file? <66.6k>

    something is missing in the example prog?

  15. #30
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fgets(imagein , sizeof(imagein) , bmpInput) ;
    > fgets(imageout , sizeof(imageout) , bmpOutput) ;
    Why are you trying to read the filename from bmpInput and bmpOutput? These should be read from stdin, or do like I did and hardcode the input and output filenames into the program.
    Code:
      fgets(imagein , sizeof(imagein) , stdin) ;
      fgets(imageout , sizeof(imageout) , stdin) ;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. Difference between getch() and getchar()
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-04-2004, 02:34 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM