Hi,
I'm writing some code that can read some data from a bitmap file. The code works fine, but when I debug the code in Visual Studio (2010) it seems that argc and argv are not initialized properly (and actually the variable 'filename' isn't read properly either) if I read from the file more than once with my function getImageInfo (I don't pass any arguments to the program yet so argc should simply be initialized to 1). Now argc is set to 32 every time.
main program:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<alloca.h>
#include"functions.h"
#include<string.h>
int main(int argc, char* argv[]) {
FILE *bmpInput;
char filename[20];
int nRows, nCols;
printf("What is the name of the file?\n");
gets(filename);
if((bmpInput = fopen(filename, "rb")) == NULL)
{
printf("Can not read BMP file");
exit(0);
}
nCols = getImageInfo(bmpInput, 18, 4);
printf("Width: \t\t%d\n", nCols);
nRows = getImageInfo(bmpInput, 22, 4);
printf("Height: \t%d\n", nRows);
fclose (bmpInput);
return 0;
}
If I comment the line 'nRows=...' argc and argv are properly initialized and 'filename' is read properly according to the Debugger.
function:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<alloca.h>
#include<fftw3.h>
#include"functions.h"
#include<string.h>
int getImageInfo(FILE* inputFile, long offset, int numberOfChars)
{
unsigned char *ptrC;
int value=0;
unsigned char dummy;
int i;
ptrC = malloc(sizeof(unsigned char));
if (ptrC == NULL)
{
fprintf(stderr, "out of memory\n");
return 0;
}
/* Read at byte offset in the file */
fseek(inputFile, offset, SEEK_SET);
for(i=1; i<=numberOfChars; i++)
{
fread(ptrC, sizeof(unsigned char), 1, inputFile);
value = value + (int)((*ptrC)*(pow(256, (i-1))));
}
return(value);
}
I assume something must go wrong in the function, but I can't figure out what. The strange thing is that the code just runs fine, I just happened to notice this when I was debugging. I hope that someone can tell what I am doing wrong. Thanks in advance.
Best regards, Michiel.