Problem executing C program in Visual C++ 6.0

This is a discussion on Problem executing C program in Visual C++ 6.0 within the C++ Programming forums, part of the General Programming Boards category; Hello, I've been having problems running the following C program for reading binary file in Visual C++ 6.0. But if ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Problem executing C program in Visual C++ 6.0

    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

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    for starters, this is wrong
    Code:
    			ch=&c;
    			var=atoi(ch);
    atoi expects a null-terminated string, which the above is not. all you need to do is this (which may not work on all operating systems, but it does in MS-Windows and *nix.
    Code:
        var = c - '0';
    Last edited by Ancient Dragon; 10-10-2005 at 06:09 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > while(!feof(file))
    Read the FAQ as to why using feof() to control loops is bad.

    Also, you say the file is "binary", but the way you're handling the data suggests that it is merely a text file full of '0' and '1' characters with the occasional newline. If you load the file into a text editor, do you see the pattern you posted here (or a bunch of weirdness)?


    > int temp[ROWMAX][COLMAX];
    I see no point for this variable, you may as well read into the image array.

    Also, you should compare your array subscripts in readImage with ROWMAX and COLMAX to make sure you're not overflowing the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Executing C Program in Visual Studio 2008
    By rory-uk in forum C Programming
    Replies: 29
    Last Post: 07-10-2009, 01:53 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Executing a program
    By trancedeejay in forum C Programming
    Replies: 7
    Last Post: 03-06-2006, 07:55 AM
  4. easy visual c++ program problem,
    By DAIALOS in forum C++ Programming
    Replies: 2
    Last Post: 01-10-2003, 11:38 AM
  5. Linker problem in Visual C++ 6.0
    By Christer in forum Windows Programming
    Replies: 1
    Last Post: 12-10-2002, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21