By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,040 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,040 IT Pros & Developers. It's quick & easy.

Problem with scanning

P: 5
I need to write a method that will load a bitmap image into an array, but whenever I try it doesn't prompt me for the name, and if I type something in there, it will prompt me but will only look at what I typed before, here is the code thus far:
Expand|Select|Wrap|Line Numbers
  1. [printf("Enter filename: "); scanf(" %s", &fileName); load(fileName, imgSet, n);
  2. void load(char fileName[], unsigned char imgSet[], int n)/*LOADS THE IMAGE FROM A FILE NAME [STATUS: TESTING]*/
  3. {
  4.     readImage(fileName, imgSet[n]);//CALLS THE readImage METHOD FROM bitmap.c FOR THE SIZE OF THE ARRAY
  5.     if(n<10)
  6.     {
  7.         readImage(fileName, imgSet[n]);
  8.         ++n;
  9.         printf("File %s has %d rows and %d columns\n",fileName, vSize+1, hSize+1);
  10.     }
  11.     if(n==10)
  12.     {
  13.         n=0;
  14.         readImage(fileName, imgSet[n]); ++n;
  15.         printf("File %s has %d rows and %d columns\n", fileName, vSize+1, hSize+1);
  16.     }
  17.     menu();
  18. }]
Here is another part of the code
Expand|Select|Wrap|Line Numbers
  1. [
  2. #include <stdio.h>
  3.  
  4. #define LENIMG 2+256*256
  5.  
  6. typedef unsigned char    uchar;    /* unsigned type aliases */
  7. typedef unsigned short    ushort;
  8. typedef unsigned long    ulong;
  9.  
  10. typedef struct {                /* bmp header (and info) structure */
  11.                             /* header section */
  12.     char    id[2];            /* must be "BM" */
  13.     ulong    fileSize;            /* file size in bytes */
  14.     ushort    reserved[2];        /* reserved (always 0) */
  15.     ulong    totHeadSize;        /* total head size (assume 54L=0x36) */
  16.                         /* info section */
  17.     ulong    headerSize;            /* header size (assume 40L=0x28) */
  18.     ulong    numCols;            /* number of columns in image */
  19.     ulong    numRows;            /* number of rows in image */
  20.     ushort    planes;            /* number of planes (assume 1) */
  21.     ushort    bitsPerPixel;        /* bits per pixel (assume 24) */
  22.     ulong    compression;        /* compression (assume 0L=none) */
  23.     ulong    imageSize;            /* total pixels in image */
  24.     ulong    xPelsPerMeter;        /* x pixels/meter (assume 2925L=0x0b6d) */
  25.     ulong    yPelsPerMeter;        /* y pixels/meter (assume 2925L=0x0b6d) */
  26.     ulong    numLUTentry;        /* number of LUT used (assume 0L) */
  27.     ulong    impColors;            /* number of LUT important (assume 0L) */
  28. } BMPHead;
  29.  
  30. static ushort getShort(FILE *inf)
  31. {
  32.     char c;
  33.     ushort ip;
  34.     c = getc(inf);  ip = (ushort)c;        /* store lower order byte */
  35.     c = getc(inf);  ip |= (ushort)c << 8;    /* store higher order byte */
  36.     return ip;
  37. }
  38.  
  39. static ulong getLong(FILE *inf)
  40. {
  41.     char c;
  42.     ulong ip;
  43.     c = getc(inf);  ip = (uchar)c;        /* store lower order byte */
  44.     c = getc(inf);  ip |= (uchar)c << 8;
  45.     c = getc(inf);  ip |= (uchar)c << 16;
  46.     c = getc(inf);  ip |= (uchar)c << 24;    /* store highest order byte */
  47.     return ip;
  48. }
  49.  
  50. void readImage(const char *fname, unsigned char *img)
  51. {
  52.     FILE *inf;            /* input .bmp file */
  53.     BMPHead h;            /* file header */
  54.     int nRows, nCols, pos;
  55.     int row, col;
  56.     int total, gray;
  57.     uchar r, g, b, x;
  58.     int pad, p;
  59.  
  60.     inf = fopen(fname, "r");    /* open file for reading */
  61.     if (inf == NULL) {
  62.         printf("File %s: cannot open file for reading\n", fname);
  63.         exit(1);
  64.     }
  65.  
  66.     h.id[0] = getc(inf); h.id[1] = getc(inf);   /* read magic number */
  67.     if (h.id[0] != 'B' || h.id[1] != 'M') {
  68.     printf("Illegal magic number.  May not be a valid .bmp file\n");
  69.         exit(1);
  70.     }
  71.  
  72.     h.fileSize        = getLong(inf);        /* read file size */
  73.     h.reserved[0]    = getShort(inf);            /* (ignored) */
  74.     h.reserved[1]    = getShort(inf);            /* (ignored) */
  75.     h.totHeadSize    = getLong(inf);            /* total header size (assume 54) */
  76.     h.headerSize    = getLong(inf);            /* header size (assume 40) */
  77.     h.numCols        = getLong(inf);        /* number of columns in image */
  78.     h.numRows        = getLong(inf);        /* number of rows in image */
  79.     h.planes        = getShort(inf);        /* number of planes (1) */
  80.     h.bitsPerPixel    = getShort(inf);        /* (assume 24) */
  81.     if (h.bitsPerPixel != 24) {
  82.         printf("We only support uncompressed 24-bit/pixel images\n");
  83.         exit(1);
  84.     }
  85.     h.compression    = getLong(inf);            /* (assume 0) */
  86.     h.imageSize        = getLong(inf);        /* total pixels in image */
  87.     h.xPelsPerMeter    = getLong(inf);        /* (ignored) */
  88.     h.yPelsPerMeter    = getLong(inf);        /* (ignored) */
  89.     h.numLUTentry    = getLong(inf);            /* (ignored) */
  90.     h.impColors        = getLong(inf);        /* (ignored) */
  91.  
  92.     nRows = h.numRows;
  93.     nCols = h.numCols;
  94.  
  95.     if (nRows > 256 || nCols > 256) {
  96.         printf("Image size is too big\n");
  97.         exit(1);
  98.     }
  99.  
  100.     img[0] = (nRows-1);
  101.     img[1] = (nCols-1);
  102.  
  103.     pad = 4*ceil((float)nCols*3/4) - nCols*3;
  104.     for (row = 0; row < h.numRows; row++) {/* read pixel values */
  105.         pos = row*nCols + 2;
  106.         for (col = 0; col < h.numCols; col++) {
  107.         b = getc(inf);  g = getc(inf);  r = getc(inf);/* read RGB */
  108.         total = (int)b + (int)g + (int)r;
  109.         gray = floor((float)total/3 + 0.5);
  110.         img[pos++] = gray;
  111.     }
  112.         for (p=0; p<pad; p++) x = getc(inf);
  113.     }
  114.     fclose(inf);
  115. }
  116. ]
Oct 8 '10 #1
Share this Question
Share on Google+
3 Replies


P: 29
Please put your code in tags and make comments explaining your code so I will help.
Oct 8 '10 #2

P: 5
My code is too long for tabs but here is a sample of it:

Expand|Select|Wrap|Line Numbers
  1. {printf("Enter filename: "); scanf(" %s", &fileName); load(fileName, imgSet, n);}/*Loads the image into the array for printing*/
  2.  
  3. void load(char fileName[], unsigned char imgSet[], int n)/*LOADS THE IMAGE FROM A FILE NAME [STATUS: TESTING]*/
  4. {
  5.     /*ERROR: ONCE OPTION 'L' IS ENTERED, DOES NOT PROMPT. INSTEAD THE FILE NAME MUST BE TYPED THEN
  6.      * AND THEN PROMPTED, THOUGH IT WILL USE THE ONE WHICH WAS TYPED WITHOUT BEING PROMPTED*/
  7.     readImage(fileName, imgSet[n]);//CALLS THE readImage METHOD FROM bitmap.c FOR THE SIZE OF THE ARRAY
  8.     if(n<10)
  9.     {
  10.         readImage(fileName, imgSet[n]);
  11.         ++n;
  12.         printf("File %s has %d rows and %d columns\n",fileName, vSize+1, hSize+1);
  13.     }
  14.     if(n==10)
  15.     {
  16.         n=0;
  17.         readImage(fileName, imgSet[n]); ++n;
  18.         printf("File %s has %d rows and %d columns\n", fileName, vSize+1, hSize+1);
  19.     }
  20.     menu();
  21. }
menu and readImage functions are already defined, hope this helps clarify things.
Oct 9 '10 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
scanf stops scanning when it sees whitespace (like an enter key). It leaves the enter key in stdin. Then the next scanf sees that enter key and quits without fetching anything.

Be sure to fetch that enter key before doinmg the next scanf.

This is based on the fact that scanf is for formatted input. That is, you know at the time you write the code what the inut will be. If you don't know that, then don't use scanf.
Oct 9 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.