[ C language] Symbolic I/O and Redirection (Chapter 8)


The eighth chapter in Steven Prata’s book “C Primer Plus” (in the new edition it is called “Character I/O and Input Validation”).

EOF (end of file) – designation of the end of the file. Ctrl+Z on Windows, Ctrl+D on UNIX.

Users do not really think about how word processors work, like notepad or Word. And they work thanks to the so-called. echoing the input, example:

#include <stdio.h>
int main(void)
{
int ch;
while ((ch = getchar()) != EOF) // work until ^Z (Ctrl+Z)
    putchar (ch) ;
return 0;
}

Input types:

  • Buffered input (stores input in a buffer and can be accessed after the input itself; including character by character, for example, using scanf)
    • fully buffered I/O — the buffer is flushed (its contents are sent to the destination) when it is full; usually with file input. The buffer size varies by system, but the most common values are 512 and 4096 bytes.
    • line-buffered I/O — flushed when a newline character appears.
  • Unbuffered (direct) input (default on some older systems; input text can be displayed on the screen immediately; used for interactive applications when you want the command to be executed immediately after pressing a key, for example, in games)

With the advent of ANSI C, switching to unbuffered input is no longer done in the language and depends on the specific operating system (there are setbuf and setvbuf, but there are limitations).

Input and output redirection and files

Output to file (>) prog >file1
Input from file (<) prog <file2
Combined redirect
prog<file2>file1
prog >file1 <file2

Here I finish the summary and want to move on to the example programs.

Number Guessing Program: You choose a number, and the computer tries to guess it.. for starters, in the most antediluvian way.

The first noob implementation, in which I made a lot of extra:

// PC is trying to guess a number. is it 6? -> n -> ++
#include <stdio.h>
int main (void)
{

int x=6;
int n=0;
char ch;

printf("I'll try to guess your number. 'y' if I'm right,\
'n' if I'm wrong\n");

while (1)
{
 printf("Is it %d?\n", n);
 while ((ch = getchar()) == '\n')
    continue;
 if (ch == 'y')
 {
    printf("Hurray!\n");
    getchar();getchar();
    return 0;
 }
 if (ch != 'y')
    n++;
}
getchar();
}

Let’s remove the noodles:

#include <stdio.h>
int main (void)
{

int n=1;

printf("I'll try to guess your number. Say 'y' if I'm right\n");
printf("Hm.. Let me think.. Is it %d?\n");
while (getchar() != 'y')
{   while (getchar() != '\n')
        continue;
    printf("Is it %d?\n", ++n);
}
printf("Hurray! I did it!\n");

getchar();
return 0;
}

I did not add the option with y \ n through the condition, because in my opinion, now the program works more interesting (however, this is done elementary).

Next… a program that prints characters in rows and columns:

/* Enter a character and two integers:
c 1 2
cc
Enter another character and two integers;
Enter a newline to quit.
... */

#include <stdio.h>
#include <ctype.h>

void display (char ch, int rows, int cols);

int main (void)
{

int ch;
int rows, cols;

printf("Enter a character and two integers:\n");

while ((ch = getchar()) != EOF)
{
 if (!isspace(ch))
  {
    if (scanf("%d %d", &rows, &cols) != 2)
        break;
    display (ch, rows, cols);
    printf("Another values or EOF sequence to quit:\n");
  }
}

getchar();
return 0;
}

void display (char ch, int rows, int cols)
{

int n, i;

 for (n = 0; n<rows; n++)
    {
     for (i = 0; i<cols; i++)
         printf("%c", ch);
     printf("\n");
    }
 printf("\n");
}

……

Ahem… At this point, I paused for a month. Haven’t done programming in a month. What the video was about:

(video link coming soon)

Now I’m back (it’s hard as hell) and my first program:

#include <stdio.h>
#include <ctype.h>

int main (void)
{

char ch;

printf("How are you?\n");
printf("a) super    b) good\n");
printf("c) not bad  d) so so..\n");
printf("'q' to quit :)\n");

while ((ch = getchar()) != 'q')
{
    if (isspace(ch))
        continue;

    switch (ch)
        {
        case 'a':
            printf ("Yay!\n");
            break;
        case 'b':
            printf ("Fun :)\n");
            break;
        case 'c':
            printf ("Ok. \n");
            break;
        case 'd':
            printf ("Sad..\n");
            break;
        default:
            printf ("Enter something else please..\n");
            break;
        }

    printf("How are you?\n");
    printf("a) super    b) good\n");
    printf("c) not bad  d) so so..\n");
    printf("'q' to quit :)\n");

}

return 0;
}

Well, you have to start somewhere 🙂 This is the program that is given in the “Menu Organization” section; I wrote it without peeping, in order to at least remember a little what and how.

So let’s continue…


This entry was posted in C language (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.