Article View: alt.msdos.programmer
Article #1168Re: Two C problems
From: William Hutto
Date: Mon, 08 Jan 1996 11:45
Date: Mon, 08 Jan 1996 11:45
251 lines
8612 bytes
8612 bytes
In article <4cojnn$rgd@lugb.latrobe.edu.au>,
csigjb@luxor.latrobe.edu.au () wrote:
>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.
There isn't a problem with the C language. You just need to build up to where
you want to be (apparantly as competant in C as you consider yourself to be in
Pascal). I just scrolled through your code without even stopping and noticed
you passed up the switch() statement for else if() for example. Be more
thorough in your studies. An example:
switch(ch) {
case up:
/*do the up thing*/
break;
case down:
/*get down!*/
break;
case sideways:
...
/*and so forth*/
I figure you're competant enough. Just slow down and learn the syntax and
constructs. There's not many. Some of them are gonna be different from what
you're used to. The biggest part in terms of memorization is learning the
standard library functions. It's a good language, you just have to get over
your bias for Pascal.
Bill
"Whatcha got on?...Your mind?"
Technical Details
Message-ID:
<4cr051$1quo@news.gate.net>Path:
arc1-ca.pugleaf.net!gate.net!pslfl2-10References:
<4cojnn$rgd@lugb.latrobe.edu.au>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!gate.net!pslfl2-10 From: bhutto@gate.net (William Hutto) Newsgroups: alt.msdos.programmer,comp.lang.c Subject: Re: Two C problems Date: 8 Jan 1996 11:45:37 GMT Organization: CyberGate, Inc. Message-ID: <4cr051$1quo@news.gate.net> References: <4cojnn$rgd@lugb.latrobe.edu.au> NNTP-Posting-Host: pslfl2-10.gate.net X-Newsreader: News Xpress Version 1.0 Beta #4