Teen Programmers Unite  
 

 

Return to forum top

Graphics in BC++

Posted by Cortez [send private reply] at August 24, 2002, 08:56:21 AM

I included graphics.h and tried some stuff with it, but for some reason, it gives error. Does anyone know how to get it work (ie, can someone post a very simple main function with a simple graphic function)? Or is there anything I should add as parameter to BCC32?

Posted by gian [send private reply] at August 24, 2002, 09:43:39 AM

Perhaps, as is so often suggested when posting problems, that you describe the errors you encountered in some amount of detail?

Posted by Cortez [send private reply] at August 26, 2002, 06:28:57 AM

Well, you're absolutely right. I'll post the error here soon. But before that, can someone tell me where's the error in this function?

==================
void encode(char input[128], char output[128])
{
FILE *inputf, *outputf;
register char A[8],B[8],C[8],D[8],E[8],F[8],G[8],H[8],OUT[64];
register char i,j;

inputf = fopen(input,"rb");
outputf = fopen(output,"wb");

for(i=0; i<64; i++)
{
if(i<8) A[i] = fgetc(inputf);
else if(i>=8 && i<16) B[i-8] = fgetc(inputf);
else if(i>=16 && i<24) C[i-16] = fgetc(inputf);
else if(i>=24 && i<32) D[i-24] = fgetc(inputf);
else if(i>=32 && i<40) E[i-32] = fgetc(inputf);
else if(i>=40 && i<48) F[i-40] = fgetc(inputf);
else if(i>=48 && i<56) G[i-48] = fgetc(inputf);
else if(i>=56 && i<64) H[i-56] = fgetc(inputf);

//if(feof(inputf)) break;
}
j=0;
for(i=0; i<8; i++)
{
OUT[j] = A[i]; j++;
OUT[j] = B[i]; j++;
OUT[j] = C[i]; j++;
OUT[j] = D[i]; j++;
OUT[j] = E[i]; j++;
OUT[j] = F[i]; j++;
OUT[j] = G[i]; j++;
OUT[j] = H[i]; j++;
}

fprintf(outputf,"%s",OUT); //something's wrong...
}
==============
As you see, this is programed for a 64 byte file. I tried this with a 64 byte text file, but the output file is 67 byte! How can this be? I mean, OUT is a 64 char array. Where did the last three silly chars came from?

Posted by Psion [send private reply] at August 26, 2002, 08:08:51 AM

That function is goofy in several ways. First, you are using the "register" keyword, which you shouldn't with a modern compiler that can optimize on its own. I'd be surprised if your PC has a 64 byte general purpose register available! :-D

Secondly, have you considered using a 2-dimensional array instead of that bunch of one-letter 1-D arrays?? I think it would simplify your code greatly and make it easier for you to find your problem.

Thirdly, remember that strings must be zero-terminated. Oops, I think I just gave it away! You really want to be using fwrite instead of fprintf, anyway.

Posted by Cortez [send private reply] at August 26, 2002, 10:08:48 AM

Sorry if I misunderstand due to my poor English, but are you offending me?

I am not a C programmer, I am a BASIC programmer(formerly DarkBasic). I bought a C book and read about registers there. It says, registers are limited but about 2 times faster. I never say I know C or I am an expert. I was just planning to expland this text conversion to 3D (then I'll use multi-dim arrays) but when I try a simple 2D version, it ouput something different that I expected. I first tried

printf("%s",OUT);

But the same result. Is fwrite going to fix it, or do I get to use

strcpy(A,"")

for each array?

Posted by CViper [send private reply] at August 26, 2002, 10:55:05 AM

as psion suggested, use fwrite( OUT, 1, 64, outputf ); that makes sure you only got 64 bytes in you file :)

Secondy what psion said about the registers is true; your compiler (if it's a new one) will most likely simply ignore the register keyword. Unless you've got a 64 bit machine, even a singe char a[8] won't fit in any standard registers (the biggest is 32 bits)

Posted by unknown_lamer [send private reply] at August 26, 2002, 02:13:21 PM

Well, remember than [] is just a way to specify a pointer that is optionally statically allocated. So the pointer to the first element would be stored in the register. The compiler would probably already do this if it was used enough.

Posted by CViper [send private reply] at August 26, 2002, 02:27:39 PM

yeah. my fault :/

Posted by Psion [send private reply] at August 26, 2002, 04:12:01 PM

Cortez, no, I'm not trying to insult you. I'm just pointing out major flaws in your programming style so that you can correct them.

unknown_lamer, automatic arrays won't be handled that way by a real compiler, though issues like stack vs. heap storage are, of course, not specified by the C standard. Local arrays will really be allocated as simply a sequence of bytes on the stack, and the pointer that you get from using the name of an array will be calculated by adding an offset to the address of the beginning of the current stack frame.

Posted by gian [send private reply] at August 27, 2002, 02:35:53 AM

Psion wouldn't let me forget for 2 weeks the fact that I used ^ as exponent as opposed to XOR, accidentally, so in comparison he was paying you a compliment.

Posted by Cortez [send private reply] at August 27, 2002, 06:27:27 AM

Well, thanks you all guys. I finally got it worked:

void encode(char input[128], char output[128])
{
FILE *inputf, *outputf;
register char A[8],B[8],C[8],D[8],E[8],F[8],G[8],H[8],OUT[64];
register signed char i,j,m;

inputf = fopen(input,"rb");
outputf = fopen(output,"wb");
while(1)
{
m=0;
for(i=0; i<64; i++)
{
if(i<8) A[i] = fgetc(inputf);
else if(i>=8 && i<16) B[i-8] = fgetc(inputf);
else if(i>=16 && i<24) C[i-16] = fgetc(inputf);
else if(i>=24 && i<32) D[i-24] = fgetc(inputf);
else if(i>=32 && i<40) E[i-32] = fgetc(inputf);
else if(i>=40 && i<48) F[i-40] = fgetc(inputf);
else if(i>=48 && i<56) G[i-48] = fgetc(inputf);
else if(i>=56 && i<64) H[i-56] = fgetc(inputf);
if(feof(inputf)) break;
m++;
}
j=0;

for(i=0; i<m/8; i++)
{
OUT[j] = A[i]; j++;
OUT[j] = B[i]; j++;
OUT[j] = C[i]; j++;
OUT[j] = D[i]; j++;
OUT[j] = E[i]; j++;
OUT[j] = F[i]; j++;
OUT[j] = G[i]; j++;
OUT[j] = H[i]; j++;
}
if(feof(inputf)) break;
fwrite(OUT,64,1,outputf);
}
}

Unfortunately, as you see, this works only for 64k kb files, and that's what i'm gonna fix so.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.