Teen Programmers Unite  


Return to forum top

Lame C++ Questions

Posted by Cortez [send private reply] at July 11, 2002, 06:10:05 AM

This is my first post here. I am sorry if I sound lame ^^ I am a newbie in C. I have MS Quick C 1.0. I was looking for a higher version. [2.5 maybe].

And can someone translate this QBASIC decoder to C?

function decode(input$, output$)

open input$ for binary as #1
open output$ for binary as #2


txt$ = " "
get #1, ,txt$
mew% = asc(txt$)

if mew% = 0 then mew2% = 12
if mew% = 1 then mew2% = 16
if mew% = 2 then mew2% = 24

rem and it goes on like that till 255...

txt2$ = chr$(mew2%)
put #2, ,txt2$

counter = counter+1

loop until counter = LOF(1)

close #1
close #2

end function

Thanks is advance ^^

Posted by sphinX [send private reply] at July 11, 2002, 07:06:25 AM

Perhaps you should take a look at the C++ Learning Group for links to tutorials and information. I assure you, if you go through the first few lessons of any C tutorial you will most probably have 90% of the knowledge you need to actually rewrite that program yourself.

Posted by Psion [send private reply] at July 11, 2002, 08:13:11 AM

And you should get some version of gcc to use instead of "Quick C".

Posted by RedX [send private reply] at July 11, 2002, 08:22:30 AM

I'll give some hints to get you started (after you got through the C tutorial as sphinx said)

open will be FILE *fopen(const char *filename, const char *modus)

modus is a string containing 1 or 2 letters:
r read ASCII
w write ASCII
a append ASCII
To use binary files add a "b" after the letter : "rb", "wb"

close will be int fclose(FILE *file)
get will be char *fgets(char *string, int lenght, FILE *file)

All the other things you'll learn very early in a tutorial.


Posted by Cortez [send private reply] at July 15, 2002, 11:32:55 AM

Yeah, thanks, I know. I wrote the C version and tested it on a txt file containing the word "abc". But when I tried it with a bigger file (an FAQ file) the output size is reduced about 52KB. Is char type limited with 127? Or do I get to post the current C source?

Posted by RedX [send private reply] at July 15, 2002, 03:39:03 PM

a char can hold -128 to +127, an unsigned char can hold 0 to 255. It's one byte.


Posted by unknown_lamer [send private reply] at July 15, 2002, 04:36:07 PM

Actually, a char can either be signed or unsigned and unless you explicitely say which, you don't know. Some systems use unsigned, and just as many use signed. Unless you use the signed or unsigned keyword, you don't have any guarantee that the char is signed.

Posted by Cortez [send private reply] at July 16, 2002, 10:52:45 AM

I used MS QC 1.0. I know that char is up to 127, but is there a way to grab from file to a int? [would this fix the problem?]

Posted by unknown_lamer [send private reply] at July 16, 2002, 11:36:52 AM

What? A file descriptor is an int if that is what you mean. You can also store the value of a char into an int if you want (e.g. int foo = some_char;).

Posted by vladimir_l [send private reply] at July 16, 2002, 11:41:55 AM

Quick C Scares me ... Cortez I can send you floppy-size files of Quick C 2.5 if you want they should be on my Programirovanie 99 disk ... or download them from almost any website. I like old software , but there is an extend , like I would not Use Mosaic Communication 0.7b you should get something more advanced that QC1.


Posted by unknown_lamer [send private reply] at July 16, 2002, 11:57:01 AM

Why not just use GCC? DJGPP and cygwin are both Free (as in beer and speech).

Posted by vladimir_l [send private reply] at July 16, 2002, 11:59:10 AM

I know but he might be intent on QC .... I havent used it for ages but I still have it.


Posted by RedX [send private reply] at July 16, 2002, 01:41:36 PM

getc reads one char. I think yoo should use fread() to read integers.

size_t fread( (void) *pointer_to_buf, size_t var_size, size_t var_amount, FILE *file);

pointer_to_buf is a pointer to variable to store the data.
var_size is the size of the variable to read.
var_amount is how many variables need to be read.
(size_t is an unsigned int)

So to read one int:

int result;
int var;
FILE *file = fopen(...)

result = fread( (void) &var, sizeof(int), 1, file);

An other way would be to read the low and high byte of the int with getc() and use bitshifts and an add to put both bytes in one int.

int result;
char low;
char high;

low = getc(file);
high = getc(file);

result = low + (high << 8);

Problem is which byte to read first. There are 2 ways how multibyte variables are stored: little endian and big endian.
Little endian stores the low byte first, big endian the high byte.

I think windows uses little endian and Unix big endian. But it could be the exact opposite.

(This is why I prefere to use the C++ I/O streams)


Posted by unknown_lamer [send private reply] at July 16, 2002, 02:03:38 PM

Endianess is a processor not operator system attribute. The x86 is little endian and the PPC is big endian. I talked to an old time UNIX guy about a month ago and he said that big endian was better because of something about memory controllers...but that doesn't really matter.

Your code will have some problems because ints are usually 32-bits because the 386+ are all 32-bit processors (and the int is supposed to be the "natural" size for the processor). 32-bits = 4 bytes, so you can't just read 2 bytes into the integer. An int might be 16-bits when running DOS (since the processor is running in 16-bit real mode), but I haven't run DOS in years and never really wrote code for it.

Posted by Neumann [send private reply] at July 16, 2002, 02:22:58 PM

My Computer Internals teacher said that Big Endian is better because it makes everything readable from left to right, including array of bytes (strings) in memory. With little endian, numbers are fetched from right to left while arrays of bytes still have to be fetches from left to right.

I'm not sure that explain the things about the memory controler your UNIX guy said but that sure makes things a little more complicated.

Posted by RedX [send private reply] at July 16, 2002, 03:27:21 PM

Actually the size of an int depends on the compiler. (short is always 2 bytes and long always 4 bytes). And QC is a 16bit compiler. Its output is a real-mode program. DJGPP for example produces 32bits protected mode programs. In the latter case an int will be 32bits, but in the first not.


Posted by Cortez [send private reply] at July 17, 2002, 04:48:43 AM

Telling the truth, I don't like MS very much. The reason for me to use something like QC is that it is small, i have 33K connection :( If you have QC 2.5, please send it to hexiumpdc@yahoo.com [if not bigger than 3MB] I can't reach the .c file now, I'm having problems with the comp. It seems i get to reinstall windows. I'll post the source in a few hours. I hope you guys can find the mistake ^^

Posted by vladimir_l [send private reply] at July 17, 2002, 06:51:47 AM

Maybe you just need to get a good C book rather than posting code , may I sugest the famous book my Kerningham and Ritchie 2nd Edition ( published in 1988 ) which is the best book if you require to learn C. If like me you are mad about C ( which does kick some serious ass ) then you might consider getting linux or netbsd or freebsd or openbsd.


Posted by Cobbs [send private reply] at July 18, 2002, 01:57:25 AM

DJGPP isn't that big (same goes for all of the free C compilers that I've downloaded)... if you wanted all of the features anyway, you could also download them bit by bit (and it's still small!). You can find it at http://www.delorie.com/djgpp/ . It's free and not old :p .
Same for the free Borland command line tools. The C quickstart guide lists the popular free ones.

Posted by vladimir_l [send private reply] at July 18, 2002, 01:25:25 PM

Cobbs - you dont like old programming tools ?

How could you not live without old hardware + software , like 186's and Chimera ?


Posted by Cortez [send private reply] at July 19, 2002, 03:07:32 AM

Uh-oh, I will try. Here the C code. Where's the error?

#include <stdio.h>

void main() {
char inp[64], oup[64],k;

printf("\n  [  1 to Encode  ]");
printf("\n  [   2 to Quit   ]");

do {
   k = getch();

   if (k == '1') {

printf("\n\n%s"," Input   :"); scanf("%s",inp);
    printf("%s"," Output  :"); scanf("%s",oup);

   } while (k != '2');


encode(char input[64], char output[64]) {

FILE *dat1,*dat2;
register int c;
dat1 = fopen(input,"r");
dat2 = fopen(output,"w");
 c = fgetc(dat1);
 if(feof(dat1)) break;

  if(c>255) c=c-256;


Posted by unknown_lamer [send private reply] at July 19, 2002, 09:00:19 AM

Why are you incrementing c?

Posted by RedX [send private reply] at July 19, 2002, 11:28:35 AM

I tried it on DJGPP (I don't have the QC compiler) and did a few modification to it:

encode needed a return type:
void encode(char input[64], char output[64])

getch(); has to be getchar();

exit(0); replaced by return;

Added a prototype of encode() before main()

Some of these changes are only necessary on C++ compilers, but with these I got the program running.
As input I used a file containing
It returned a file with


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

Log on
Save for later automatic logon

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