Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

#include <stdio.h> #include <stdli

Status
Not open for further replies.

HsiaLin

Programmer
Apr 29, 2001
15
0
0
US
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

FILE *filename;

void main(int argc, char *argv[])
{
int rec;
int x,z;
char temp1[25];
char temp2[25];

rec=0;

if(argc>2)
{
printf(&quot;Usage: BINARY [outfile]\n&quot;);
printf(&quot;Usage: BINARY <ENTER>\n&quot;);
exit(0);
}

if(argv[1])
{
fclose(filename);
filename=fopen(argv[1],&quot;w&quot;);
if(filename!=NULL) rec=1;
if(rec==1) fprintf(filename,&quot;\nBeavis rules!\n&quot;);
}

clrscr();

for(z = 1; z < 101; z++)
{
if(z<10)
{ printf( &quot;\n DEC: 00%d |&quot;, z);
if(rec==1) fprintf(filename,&quot;\n DEC: 00%d |&quot;, z);
goto BHEAD;
}
else if (z<=99 && z>9)
{ printf( &quot;\n DEC: 0%d |&quot;, z);
if(rec==1) fprintf(filename,&quot;\n DEC: 0%d |&quot;, z);
goto BHEAD;
}
else
{ printf ( &quot;\n DEC: %d |&quot;, z);
if(rec==1) fprintf(filename,&quot;\n DEC: %d |&quot;, z);
goto BHEAD;
BHEAD:
printf(&quot; HEX: %02X |&quot;, z); if(rec==1) fprintf(filename,&quot; HEX: %02X |&quot;, z);
printf( &quot; BIN: &quot; ); if(rec==1) fprintf(filename,&quot; BIN: &quot;);}

strcpy (temp1,&quot;&quot;);
strcpy (temp2,&quot;&quot;);

for(x=15; x> 7; x--)
for( x=7; x> -1; x--)
{
printf( &quot;%i&quot;, (z & 1 << x) > 0 ? 1 :0);
sprintf(temp1,&quot;%i&quot;, (z & 1 << x) > 0 ? 1 :0);
strcat(temp2,temp1);
}
strcat(temp2,&quot;\n&quot;); //remove spaces btwn lines in outfile
printf( &quot;\n&quot; ); //removes spaces onscreen
if(rec==1) fprintf(filename,temp2);
}
if(rec==1) fprintf(filename,&quot;\r\nThank you for flying Butthead Airlines!\n&quot;);
if(rec==1) fclose(filename);
}
 
Why did you put fclose wher no files are opened for filename? You'll get an error. You should only remove that line. John Fill
1c.bmp


ivfmd@mail.md
 
I don't think that the line of fclose is a problem. However, I wouldn't write a program like that. I'd rather change the declaration line to:
FILE *filename = NULL;

Ten years ago, when I was a MS student working on a project, I read a book which indicates that it's a good habitat to initialize a variable when you declare it whenever it's possible. I said: perhaps it's, but why bother?

Later, when I tested my program, which has 100K source code, I found that it may crash sometimes. I knew that it's a tough bug. It spent me nearly one week to find out: I forgot to assign a value for a variable. It wouldn't happen if I gave it an initial value.
 
Greetinx.

1. remove line fclose(filename);
before any opening file

3. remove declaration of temp2 string and try this code to convert int to binary and output to screen any oplionally to file. It may help

for(x=0; x<7; x--)
temp1[x]=z & (1<<(6-x));
printf(temp1);
printf( &quot;\n&quot; ); //removes spaces onscreen
if(rec==1) fprintf(filename,temp1);
if(rec==1) fclose(filename);


 
Greetinx.

1. remove line fclose(filename);
before any opening file

2. remove declaration of temp2 string and try this code to convert int to binary and output to screen any oplionally to file. It may help

for(x=0; x<7; x--)
temp1[x]=z & (1<<(6-x));
printf(temp1);
printf( &quot;\n&quot; ); //removes spaces onscreen
if(rec==1) fprintf(filename,temp1);
if(rec==1) fclose(filename);


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top