Teen Programmers Unite  
 

 

Return to forum top

c entab problems

Posted by buzgub [send private reply] at December 27, 2001, 11:45:33 PM

I'm attempting to write the 'entab' program suggested by K&R. All my program does is turns my 11-character test input file into a file with 20 spaces (as opposed to the desired effect of 5 spaces, followed by an a, followed by 3 spaces becoming a tab, a space, and 3 characters).

infile is a file pointer, as is outfile. thisChar is a char, spaces and i are ints. NUMOFSPACES is defined as 4.

while ( (thisChar = getc(infile)) != EOF )
if (thisChar == ' ')
if (++spaces == NUMOFSPACES){
spaces = 0;
putc('\t', outfile);
}
else {
if (spaces != 0){
/* Put a space in for every stored space */
for (i = 0; i < spaces; i++)
putc(' ', outfile);
spaces = 0;
}
/* write the character we just saw */
putc(thisChar, outfile);
}

I've spent a while bashing my head against the thing and can't work out what's wrong. So, can anyone here tell me what bleedingly-obvious thing I'm missing?

Posted by buzgub [send private reply] at December 28, 2001, 12:26:27 AM

Well, it appears that I was labouring under the misapprehension that an "if (x > 1) y++; else {y--; x--;}" style of thing would not need to be braced. Thanks to gian over msn for that bit of enlightenment.

Posted by gian [send private reply] at December 28, 2001, 12:33:43 AM

Some curlies (}{) fixed that...

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.