473,498 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with scanning

5 New Member
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
3 1820
Alex T
29 New Member
Please put your code in tags and make comments explaining your code so I will help.
Oct 8 '10 #2
Sean Kernaghan
5 New Member
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
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

21
12167
by: CHANGE username to westes | last post by:
What are the most popular, and well supported, libraries of drivers for bar code scanners that include a Visual Basic and C/C++ API? My requirements are: - Must allow an application to be...
8
1870
by: TheB | last post by:
Ok, lets try this again. I have a program which searches all disk drives for certain file types. When it finds a file it writes a record to a Firebird DB. The program normally is using 40 - 44...
6
1281
by: trialproduct2004 | last post by:
Hi all I am having problem at a time of handling threading. I am having application containing thread. In thread procedure i ma using recursive function. This recursive function is adding some...
1
2235
by: Mantorok | last post by:
Hi all Does anyone here have to use VS on a machine that has "on-access" virus scanning? We have Read/Write scanning turned on on our desktops and we're pretty sure that it is killing VS and...
0
1989
by: deacon57 | last post by:
FYI - If you are a computer scientist (or geek), this post may be for you. I wanted to log any search keywords, etc that are used on my site. I created a simple program that logs search terms...
3
2128
by: nor | last post by:
Hi, I've application to capture video of image and grab the still image. It runs well until I put loop to regrab the still images many times automatically. It gives one exception error:...
1
1746
by: miconib | last post by:
Hello everyone. I am not really a databse pro like most of you here, but i did make a small access database for our warehouse to scan serial numbers into the system. I have a few questions if anyone...
1
1522
kirubagari
by: kirubagari | last post by:
For i = 49 To mfilesize Step 6 rich1.SelStart = Len(rich1.Text) rich1.SelText = "Before : " & HexByte2Char(arrByte(i)) & _ " " & HexByte2Char(arrByte(i + 1)) & " " _ &...
2
4303
by: iheartvba | last post by:
Hi Guys, I have been using EzTwain Pro to scan documents into my access program. It allows me to specify the location I want the Doc to go to. It also allows me to set the name of the document...
8
5422
by: pinkskink | last post by:
I have a problem using a bar code scanner into a text area. I found on this forum someone with the same problem as me, except he thought the javascript wasn't finding the <GS> character \1D....
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3081
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.