🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.msdos.programmer
9 messages
9 total messages Started by csigjb Sun, 07 Jan 1996 14:01
Two C problems
#1160
Author: csigjb
Date: Sun, 07 Jan 1996 14:01
216 lines
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.
Re: Two C problems
#1167
Author: Cameron Foster
Date: Sun, 07 Jan 1996 19:48
53 lines
1691 bytes
In <4cojnn$rgd@lugb.latrobe.edu.au> csigjb@luxor.latrobe.edu.au ()
writes: 
>
>I have two C problems.
>
>PROBLEM 1 :
>

Didn't have time to look at problem 1 yet, but as to problem 2:

>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.

Actually, the "damn language" IS working sensibly and predictably! Read
up on C escape sequences. 

int i = 27;     /* i has the decimal value of 27 */
int i = \27;    /* i has the decimal value of 23 (\27 is the octal */  
                /* representation of decimal 23) */
int i = \x27    /* i has the decimal value of 39 */
Re: Two C problems
#1168
Author: William Hutto
Date: Mon, 08 Jan 1996 11:45
251 lines
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?"
Re: Two C problems
#1169
Author: Brian Hibbert
Date: Mon, 08 Jan 1996 14:29
68 lines
2644 bytes
csigjb@luxor.latrobe.edu.au () wrote:
>I have two C problems.
>
>PROBLEM 1 :
>
Snip....

I didn't try your program.  I can't answer question 1, but I can answer
question 2.

>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?

C is not using a different character table.  The format string you are 
using is using a different radix.  According to my documentation, if you
specify an escape sequence in the format \nnn (where nnn is a 1 to 3 
digit number) the number is taken as an OCTAL number.  OCTAL 13 = decimal 
11 and OCTAL 27 = decimal 23.  Your program is looking for the wrong 
values.  You can also specify a number using hexadecimal notation by 
using the \xnn escape sequence (where nn is 1 or 2 hex digits).



>
>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.

The language has been rewritten.  The rewrite is called C++.  The C++ 
language still allows you to build broken programs using the C syntax, 
but it is more strongly typed than plain C and adds new features that 
allow you to avoid many problems altogether (for example the << output
operator for the I/O classes simplifies formatted output and lets you get 
away from printf() and format strings).



-- 
***********************************************
Brian Hibbert  b_hibbert@csc32.enet.dec.com
Digital Equipment Corporation
All opinions expressed are my own and should
not be assumed to be the opinion of my employer
Re: Two C problems
#1170
Author: John R Buchan
Date: Mon, 08 Jan 1996 17:16
355 lines
11469 bytes
In article <4cojnn$rgd@lugb.latrobe.edu.au>, csigjb@luxor.latrobe.edu.au 
says...
>
>I have two C problems.

The following is a reformatted (sorry, I prefer my format) version of the 
full version of the code that was originally posted (sorry about the rather 
large post folks).  I have made a few modifications (I think I remembered to
add comments by each.  Please excuse those that you have already corrected 
in your second post) and it now seems to be functional (though I can't be 
certain that it functions the way you expected).  BTW, You might want to 
consider breaking your main into smaller functions.

As a few people have probably mentioned, it is considered rather poor taste
to post non-ansi code in c.l.c and even worse to post incomplete uncompilable
code.  Always try to post a minimum compilable example (as little non-ansi 
code as possible) that illustrates your problem.  It is sometimes easier to
write a new small program for this rather than trying to edit the original
(just be sure that it probuces the same problem)  A consistant format and
well placed comments are also helpful.

#include <stdio.h>
#include <stdlib.h>  /* needed for ansi exit() and EXIT_FAILURE */
#include <conio.h>
#include <string.h>
#include <alloc.h>
#include <process.h>

/* it is fairly common practice to use all caps in the names of globals. */
#define  maxstring 127 /* this should be a constant expression not a const */
const lineend='\n';
const escape=27;
const null='\0';     /* Using nul for this name will cause less confusion. */
const space=' ';
const up='H';
const down='P';
const pageup='I';
const pagedown='Q';
const left='K';
const right='M';
const home='G';
const end_='O';


typedef char string[maxstring]; /* needs a constant expression, not a const */
typedef struct nodetypetag
   {
   string line;
   int linenum;
   struct nodetypetag *next,*previous;
   } nodetype;

typedef nodetype *nodetypeptr;

string line,line_,line1,bottomline;

char empty(nodetypeptr posptr)
   {
   return(posptr==NULL);
   }

void initialize(nodetypeptr *listptr)
   {
   *listptr=NULL;
   }

void add(nodetypeptr *listptr,char *fileline,int Linenum)
   {
   nodetypeptr newptr,tempptr;

   newptr=(nodetypeptr)malloc(sizeof(nodetype));
   /* need to verify allocation success */

   strcpy(newptr->line,fileline);
   newptr->linenum=Linenum;
   newptr->next=NULL;  /* no need to write this twice */
   if (*listptr==NULL)
      {
      (*listptr)=newptr;
/*      (*listptr)->next=NULL;  not needed */
      (*listptr)->previous=NULL;
      }
   else
      {
      tempptr=*listptr;
      while (tempptr->next!=NULL)
         {
         tempptr=tempptr->next;
         }
      tempptr->next=newptr;
/*      newptr->next=NULL;   not needed */
      newptr->previous=tempptr;
      }
   }

/* void remove(nodetypeptr *listptr) name is in conflict with the ansi */
/* function remove, which is prototyped in stdio.h                     */
void RemoveNode(nodetypeptr *listptr)
   {
   nodetypeptr tempptr;

   /* NULL tests missing */
   if( listptr != NULL && *listptr != NULL) /* at least 1 node exists */
      {
      tempptr=*listptr;
      *listptr=(*listptr)->next;
      (*listptr)->previous=NULL;
      free(tempptr);
      }
   }

/* This would probably work better as:                               *
*                                                                    *
*  nodetypeptr setpos( int linenum)                                  *
*                                                                    *
*  This would reduce the number of indirects and allow you to return *
*  NULL, if an invalid number manages to be passed in.               */
void setpos(nodetypeptr *posptr,int linenum)
   {
   while ((*posptr)->linenum!=linenum)
      {
      if ((*posptr)->linenum>linenum)
         {
         *posptr=(*posptr)->previous;
         }
      else
         if ((*posptr)->linenum<linenum)
            {
            *posptr=(*posptr)->next;
            }
      }
   }

void writescreenfull(char width,char height,nodetypeptr posptr,char stringpos)
   {
   char y;
   char *line__;  /* this definition was apparently left out */

   window(1,1,width,height);
   clrscr();
   window(1,1,width,height+1);
   y=1;
   while ((y<=height) && (!empty(posptr->next)))
      {
      gotoxy(1,y);
      strcpy(line_,posptr->line);
      line__=&(line_[stringpos]); /* line__ = line_ + stringpos; */
      strncpy(line,line__,width-1);
/*      strcat(line,null); someone else explained this already */
      line[maxstring - 1] = '\0'; /* this provides your safety */
      cputs(line);
      posptr=posptr->next;
      y++;
      }
   }

/* Since fgets will always append a '\0', there is no need for the    *
 * maxstrings parameter.  Just test for '\0' and '\n' or use the ansi *
 * strrchr function.                                                  */
void removelineends(char *strings,unsigned int maxstrings)
   {
   unsigned int i;

   i=0;
   while ((i<maxstrings) && (strings[i]!=lineend))
      {
      i++;
      }
     strings[i]=null;
   }

int main(int argc,char *argv[]) /* main must return int */
   {
   FILE *file;
   char x,ch,width,height,back,fore,stringpos;
   int numlines,linenum;
   struct text_info info;  /* forgot the struct */
   nodetypeptr listptr,posptr;


   if (argc<2)
      {
      cputs("\nShow what . . .\n");
      exit(EXIT_FAILURE); /* exit with failure value, when code fails */
      }
   else
      {
      file=fopen(argv[1],"rt");
      if (file==NULL)
         {
         cprintf("\nFile %s not found . . .\n",argv[1]);
         exit(EXIT_FAILURE); /* exit with failure value, when code fails */
         }
      else
         {
         rewind(file);  /* not needed, "r" opens to the beginning anyway */
         initialize(&listptr);
         linenum=0;
         while (fgets(line,maxstring,file)!=NULL)
            {
            removelineends(line,maxstring);
            add(&listptr,line,linenum);
            linenum++;
            }
         numlines=linenum--;  /* ?? */
         fclose(file);

         posptr=listptr;

/* This would make a nice 'InitScreen' function */
         gettextinfo(&info);
         fore=info.attribute&0x0f;
         back=(info.attribute&0x70)/0x10;
         height=info.screenheight;
         width=info.screenwidth;
         strcpy(bottomline,"  \30  \31  pgup  pgdn  s <string> "
                           "(search for string)  esc (quit)");
         for(x=strlen(bottomline)+1;x<width;x++)
            {
            strcat(bottomline," ");
            }
         /* look into sprintf */

         _setcursortype(_NOCURSOR);
         clrscr();
         textcolor(back);
         textbackground(fore);
         gotoxy(1,height);
         cputs(bottomline);
         textcolor(fore);
         textbackground(back);
/* end 'InitScreen' function */

         height--;
         linenum=0;   /* the first line in your list is 0, not 1 */
         stringpos=0; /* this should start at 0, not 1 */
         writescreenfull(width,height,posptr,stringpos);
         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=getch();
               if (ch==null)
                  {
                  ch=getch();
                  }
               }

/* This would be much clearer using a switch statement */
            if (ch==up)
               {
               if ((linenum-1)>=0)
                  {
                  linenum--;
                  }
               else
                  {
                  linenum=0;
                  }
               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) /* s/b 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; /* s/b 0, not 1 */
                                   }
            if (ch!=escape)
               {
               ch=space;
               }
            writescreenfull(width,height,posptr,stringpos);
            }
         height+=1;
         window(1,1,width,height);
         clrscr();
         _setcursortype(_NORMALCURSOR);
         while (!empty(listptr))
            {
            RemoveNode(&listptr);
            }
         }
      }
   
   return 0;
   }



-- 
John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
Re: Two C problems
#1172
Author: Lawrence Kirby
Date: Tue, 09 Jan 1996 16:47
26 lines
1278 bytes
In article <4cp82k$gsq@cloner3.netcom.com>
           cfoster1@ix.netcom.com(Cameron "Cameron Foster " writes:

>Actually, the "damn language" IS working sensibly and predictably! Read
>up on C escape sequences. 

:-)

>int i = 27;     /* i has the decimal value of 27 */

OK.

>int i = \27;    /* i has the decimal value of 23 (\27 is the octal */  
>                /* representation of decimal 23) */

No, that is illegal. For octal you write 027 or '\27'

>int i = \x27    /* i has the decimal value of 39 */

Similarly here you must have 0x27 or '\x27'

-- 
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Re: Two C problems
#1182
Author: Drock
Date: Wed, 17 Jan 1996 23:59
64 lines
2768 bytes
csigjb@luxor.latrobe.edu.au () wrote:



>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.

Well, I'm not sure why this happens but there is a couple colutions:

The Ugly way:
	Use the borland graphic interface to capture the data on the screen
before anything happens in the program. Then restore the buffer upon
exit. In c++, this can be done using a constructor/destructor. The
only problem is, If you are using Borland, then you have to use their
BGI drivers. THEY SUCK!!!!!!! 

The clean way. Write yourself a cool little routine that stores the
contents of the active page. This can be done with a pointer  that is
assigned an absolute address to video memory and treating it as an
array. Then just use the same array to restore what the screen to it's
original status upon exit. look up 

>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.

Because when you actually press the esc key, it's ASCII value is 27,
using /27 refers to esc  when using the printf stuff. The /13 is
evaluated in those particular functions but I don';t think they are
evaluated when doing a straight compare. This has happened to me
before. If you do a printf and use /13, you get a CR , but if you read
in a CR, you get 13. Think about it, it's only a byte value and should
be treated as such. 
Re: Two C problems
#1183
Author: Ron Collins
Date: Thu, 18 Jan 1996 13:57
90 lines
3786 bytes
Drock (drock@owens.ridgecrest.ca.us) wrote:
: csigjb@luxor.latrobe.edu.au () wrote:



: >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.

: Well, I'm not sure why this happens but there is a couple colutions:

: The Ugly way:
: 	Use the borland graphic interface to capture the data on the screen
: before anything happens in the program. Then restore the buffer upon
: exit. In c++, this can be done using a constructor/destructor. The
: only problem is, If you are using Borland, then you have to use their
: BGI drivers. THEY SUCK!!!!!!! 

This is your professional opinion?

: The clean way. Write yourself a cool little routine that stores the
: contents of the active page. This can be done with a pointer  that is
: assigned an absolute address to video memory and treating it as an
: array. Then just use the same array to restore what the screen to it's
: original status upon exit. look up 

I thought the original poster wanted a CLEAN screen, not the ORIGINAL
screen returned.

: >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.

: Because when you actually press the esc key, it's ASCII value is 27,
: using /27 refers to esc  when using the printf stuff. The /13 is
: evaluated in those particular functions but I don';t think they are
: evaluated when doing a straight compare. This has happened to me
: before. If you do a printf and use /13, you get a CR , but if you read
: in a CR, you get 13. Think about it, it's only a byte value and should
: be treated as such. 

No.  I have no idea what you are talking about here, but it's wrong.

The tokens "13" and "\13" (note the reverse slash) refer to two
different va
                        -- Collins --
                        
-----
The views expressed here are mine alone.

Ron Collins/Hughes Aircraft Company/M20,P20/Tucson Az 85706
rcollins@thor.tu.hac.com	collins@seagull.rtd.com
­----
lues.  The token "13" is a decimal thirteen, which (as it
happens) is the ASCII ordinal value for a carraige-return.  The token
"\13" is an _octal_ eleven, which is ASCII vertical-tab, or control-K.

The reverse-slash notation is used within strings (using either octal
notation \ooo or hex notation \xhhh) simply because it is a convenient
method of putting otherwise non-printable characters inside a string.
Re: Two C problems
#1188
Author: Mark J. Restifo
Date: Sat, 27 Jan 1996 22:57
22 lines
1144 bytes
On Wed, 17 Jan 1996 23:59:00 GMT, in alt.msdos.programmer, 
csigjb@luxor.latrobe.edu.au () wrote:

>| >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.

PMFJI, I don't do a lot of C programming, but what's wrong with a simple
CLSCR() or similar function? A simple INT 10H mode reset will also do the
trick.




--
Mark R.
===========================
mrestifo@wizvax.net
102444,3171@compuserve.com
===========================
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads