Hello,
I've been having problems running the following C program for reading binary file in Visual C++ 6.0. But if I run the same program in Borland C++ 5.02, there's no problem reading the binary file and the correct output is displayed.
I want to read a binary file consists of 50x50 dimension of data as the following:
my data:
00000000000000000...
00000000000000000...
00000000000000000...
00000000000000000...
00000000000000000...
00000011111100000...
00000011111100000...
00000111000111000...
00001101110110000...
00001100011000000...
00000000000000000...
.....
....
...
my code:
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include <windows.h>
#define ROWMAX 100
#define COLMAX 100
int image[ROWMAX][COLMAX];
int ROW=0,COL=0;
unsigned int start = ::GetTickCount();
void readImage(FILE *file,int image[ROWMAX][COLMAX]);
void displayImage(FILE *file, int row, int col,int image[ROWMAX][COLMAX]);
//-----------------------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
//variables declarations
FILE *fp, //binary input file
*f2; //output file
if((fp=fopen("br0070_1.3.bin","rb"))==NULL)
{
printf("Cannot open file input.\n");
exit(1);
}
if((f2=fopen("br0070_1.3.txt","w+"))==NULL)
{
printf("Cannot open file output.\n");
exit(1);
}
readImage(fp,image);
fprintf(f2,"**** Parker Combined Thinning ****\n\nOriginal Image\n");
fprintf(f2,"Image size = %d, %d\n",ROW,COL);
displayImage(f2,ROW,COL,image);
return 0;
}
//===============================================================================================
void readImage(FILE *file,int image[ROWMAX][COLMAX])
{
char *ch,c;
int var, j=0,i=0;
int temp[ROWMAX][COLMAX];
while(!feof(file))
{
fscanf(file,"%c",&c);
if (c!='\n' && c!=' ')
{
ch=&c;
var=atoi(ch);
temp[i][j]=var;
j=j+1;
}
else
{ j=0;
i++;
}
}
ROW=i+1;
COL=j-1;
printf("\nImage Size = %d,%d\n",ROW,COL);
printf("\nreal image\n\n");
for(i=0;i<ROW;i++)
{
for(j=0;j<COL;j++)
{
image[i][j]=temp[i][j];
printf("%d",image[i][j]);
}
printf("\n");
}
}//end readImage
//====================================================================
void displayImage(FILE *file, int row, int col,int image[ROWMAX][COLMAX])
{
int i,j,count=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
fprintf(file,"%d",image[i][j]);
if (image[i][j]==1)
count++;
}
fprintf(file,"\n");
}
fprintf(file,"\nBil object pixel(1)= %d\n",count);
}
Errors produced for different binary files are "Cannot open file input" or the file is opened but there is no data read and displayed on the screen, the image size displayed is 52, -1.
It seems that the program cannot detect the correct row and columns of the characters data there's no data saved into the 2-dim array.
In order to make the program works in Visual C++ 6.0, I have to open the binary file manually, place the cursor at the end of the file and save it again. After that I run the program again, only then the data can be read and displayed correctly...
What went wrong?How can I modify the program so that it can works in Visual C++ 6.0 correctly as in Borland C++ 5.02?
Thanks in advance