Article View: alt.msdos.programmer
Article #1160Two C problems
From: csigjb
Date: Sun, 07 Jan 1996 14:01
Date: Sun, 07 Jan 1996 14:01
216 lines
7396 bytes
7396 bytes
I have two C problems.
PROBLEM 1 :
int main(int argc,char *argv[])
{
FILE *file;
char x,ch,width,height,back,fore,stringpos;
int numlines,linenum;
text_info info;
nodetypeptr listptr,posptr;
if (argc<2)
{
cputs("\nUsage : show file name [fore ground color] [back ground color] . . .\n");
}
else
{
file=fopen(argv[1],"rt");
if (file==NULL)
{
cprintf("\nFile %s not found . . .\n",argv[1]);
}
else
{
rewind(file);
initialize(&listptr);
linenum=1;
while (fgets(line,maxstring,file)!=NULL)
{
removelineends(line,maxstring);
add(&listptr,line,linenum);
linenum++;
}
numlines=linenum--;
fclose(file);
posptr=listptr;
gettextinfo(&info);
if (argc>2)
{
getcolors(&fore,&back,argv[2],argv[3]);
}
else
{
fore=LIGHTGRAY;
back=BLACK;
}
height=info.screenheight;
width=info.screenwidth;
strcpy(bottomline," \30 \31 \33 \32 pgup pgdn s (search) help (h) esc (quit)");
for(x=strlen(bottomline)+1;x<width;x++)
{
strcat(bottomline," ");
}
_setcursortype(_NOCURSOR);
clrscr();
textcolor(back);
textbackground(fore);
gotoxy(1,height);
cputs(bottomline);
textcolor(fore);
textbackground(back);
height--;
linenum=1;
stringpos=0;
writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
ch=space;
while (ch!=escape)
{
while ((ch!=up) && (ch!=down) && (ch!=pageup) &&
(ch!=pagedown) && (ch!=escape) && (ch!=left) &&
(ch!=right) && (ch!=end_) && (ch!=home) &&
(ch!=search) && (ch!=help))
{
ch=getch();
if (ch==nul)
{
ch=getch();
}
}
if (ch==up)
{
if ((linenum-1)>=1)
{
linenum--;
}
else
{
linenum=1;
}
setpos(&posptr,linenum);
}
else if (ch==down)
{
if ((linenum+1+height)<=numlines)
{
linenum++;
}
else
{
linenum=numlines-height+1;
}
setpos(&posptr,linenum);
}
else if (ch==pageup)
{
if ((linenum-height+1)>=1)
{
linenum=linenum-height+1;
}
else
{
linenum=1;
}
setpos(&posptr,linenum);
}
else if (ch==pagedown)
{
if (((linenum+height-1)<=numlines) && ((numlines-(linenum+height-1))>=(height-1)))
{
linenum=linenum+height-1;
}
else
{
linenum=numlines-height+1;
}
setpos(&posptr,linenum);
}
else if (ch==left)
{
if (stringpos>0)
{
stringpos--;
}
}
else if (ch==right)
{
if (stringpos<=(maxstring-width))
{
stringpos++;
}
}
else if (ch==end_)
{
stringpos=maxstring-width+1;
}
else if (ch==home)
{
stringpos=0;
}
else if (ch==help)
{
displayhelp(fore,width,height);
}
else if (ch==search)
{
readstring(searchstring,bottomline,height,fore,back);
}
if (ch!=escape)
{
ch=space;
}
writescreenfull(width,height,posptr,stringpos,searchstring,fore,back);
}
while (!empty(listptr))
{
remove(&listptr);
}
height++;
window(1,1,width,height);
clrscr();
_setcursortype(_NORMALCURSOR);
}
}
return(0);
}
This program should exit such that the screen is clear with the DOS prompt
in the upper left corner, but no, it exits with a short line of text on the
first line and then the DOS prompt on the next line. I never (or rarely)
have these sort of problems with Pascal so why do they continuously crop up
with C.
PROBLEM 2 :
const escape=27;
const cr=13;
const bs=8;
while (ch!=cr)
{
.
.
.
}
while (ch!=escape)
{
.
.
.
}
If I replace the 27 with \27' or the 13 with '\13' then these loops don't
work i.e. an infinite loop results. WHY?
Also if I want to do somthing like this : puts("\24 \25"); which should
print the up and down arrows on the screen (ASCII 24 and 25), but no, it
prints some other ASCII characters. As far as I can tell C has its own
unique character table, at least for the control characters. WHY?
Sometimes I wonder whether some one needs to go back an rewrite the damn
language so that it works sensibly and predictably like Pascal can be
relied upon to do.
Technical Details
Message-ID:
<4cojnn$rgd@lugb.latrobe.edu.au>Path:
arc1-ca.pugleaf.net!luxor.latrobe.edu.au!csigjbShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!luxor.latrobe.edu.au!csigjb From: csigjb@luxor.latrobe.edu.au () Newsgroups: alt.msdos.programmer,comp.lang.c Subject: Two C problems Date: 7 Jan 1996 14:01:27 GMT Organization: La Trobe University Distribution: world Message-ID: <4cojnn$rgd@lugb.latrobe.edu.au> NNTP-Posting-Host: luxor.latrobe.edu.au Originator: csigjb@luxor.latrobe.edu.au