Unable to understand the code in the Append() in a C program [closed]

StackOverflow https://stackoverflow.com/questions/23663835

  •  22-07-2023
  •  | 
  •  

Вопрос

I am trying to read a program that I came across online. I have been trying to read and understand the program, I have understood all the lines of code except the lines given below. I would be very grateful to you all if you all could help me understand those lines. The full code can be found at this site

    while(1)
  {
   c=getch();
   if(c==19)
    goto end3;
   if(c==13)
   {
    c='\n';
    printf("\n\t");
    fputc(c,fp1);
   }
   else
   {
    printf("%c",c);
    fputc(c,fp1);
   }
  }
Это было полезно?

Решение

while(1)                  // Loop forever.
   {
   c=getch();             // read a character from stdin.
   if(c==19)              // If the character read is 'CTRL-S',
      goto end3;          //   jump to the 'end3' label.

   if(c==13)              // If the character read is '(Carriage) Return',
      {
      c='\n';             //    Set 'c' to be a C 'newline' character.
      printf("\n\t");     //    Write a 'newline' and a 'tab' character to stdout.
      fputc(c,fp1);       //    Write the value of 'c' to the fp1 stream.
      }
   else                   // If the character read is -not- '(Carriage) Return',
      {
      printf("%c",c);     //    Write the character to stdout.
      fputc(c,fp1);       //    Write the value to the fp1 stream.
      }
   }

Другие советы

Because this is some pretty ugly code, here's a much simpler re-write:

while(1)
{
    if((c=getch()) == 19)  // If Ctrl+S, end this While-Loop
    { 
        break; // Goto is EVIL!
    }

    fputc((c=='\r')? (puts("\n\t"), '\n') :      // Convert Return to \n, OR
                     (putchar(c),     c ), fp1); // Put out exactly the char that was input.
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top