User Tools

Site Tools


Sidebar

projects

uxi0 (due 20200123)
pct1 (bonus; due 20200123)
wcp1 (due 20200123)
adm0 (due 20200129)
pct2 (due 20200129)
wcp2 (due 20200129)
pbx0 (due 20200205)
pct3 (due 20200205)
wcp3 (due 20200205)
pbx1 (due 20200212)
pct4 (due 20200212)
wcp4 (due 20200212)
pbx3 (bonus; due 20200226)
bwp0 (bonus; due 20200226)
usr0 (due 20200226)
pct5 (due 20200226)
wcp5 (due 20200226)
pbx2 (due 20200304)
pct6 (due 20200304)
wcp6 (due 20200304)
upf0 (due 20200311)
pct7 (due 20200311)
wcp7 (due 20200311)
upf1 (due 20200318)
pct8 (due 20200318)
wcp8 (due 20200318)
spf0 (due 20200325)
pct9 (due 20200325)
wcp9 (due 20200325)
pwn0 (due 20200401)
pctA (due 20200401)
wcpA (due 20200401)
wpa0 (due 20200422)
pctB (due 20200422)
wcpB (due 20200422)
bwp1 (bonus; due 20200422)
gfo0 (due 20200429)
pctC (due 20200429)
wcpC (due 20200429)
eoce (due 20200513)
haas:spring2020:unix:stdin_redirection

Original code:

#include<stdio.h>
 
int main()
{
    int a, b, c, d;
    float avg;
 
    printf("Please enter the first number: ");
    scanf("%d", &a);
 
    printf("Please enter the second number: ");
    scanf("%d", &b);
 
    printf("Please enter the third number: ");
    scanf("%d", &c);
 
    printf("Please enter the fourth number: ");
    scanf("%d", &d);
 
    avg = (float)(a + b + c + d) / 4;      // force operation to be considered float, not native (int)
    printf("The average is %.2f\n", avg);  // format to 2 decimal places with the .2
 
    return(0);
}

With this code typed in, compiled, and ran, you can manually enter the 4 values and should see their average.

Conversely, we can also put our desired input in a file and feed in into our program via I/O redirection:

lab46:~/src/unix$ echo 24 > infile
lab46:~/src/unix$ echo 37 >> infile
lab46:~/src/unix$ echo 16 >> infile
lab46:~/src/unix$ echo 29 >> infile
lab46:~/src/unix$ cat infile
24
37
16
29
lab46:~/src/unix$ ./avgprog < infile

Output may seem a little messy, but that's only because it still expects actual enter keys to be hit. Notice the desired end result IS displayed.

Identical code, minus a lie:

#include<stdio.h>
 
int main()
{
    int a, b, c, d;
    float avg;
 
    fprintf(stdout, "Please enter the first number: ");
    fscanf(stdin, "%d", &a);
 
    fprintf(stdout, "Please enter the second number: ");
    fscanf(stdin, "%d", &b);
 
    fprintf(stdout, "Please enter the third number: ");
    fscanf(stdin, "%d", &c);
 
    fprintf(stdout, "Please enter the fourth number: ");
    fscanf(stdin, "%d", &d);
 
    avg = (float)(a + b + c + d) / 4;      // force operation to be considered float, not native (int)
    fprintf(stdout, "The average is %.2f\n", avg);  // format to 2 decimal places with the .2
 
    return(0);
}

Basically, a regular printf() is IDENTICAL to an fprintf() to stdout. Everything is a file, remember?

Same deal with scanf()/fscanf() from stdin. The lowercase stdin/stdout/stderr are file pointers available to us in C.

haas/spring2020/unix/stdin_redirection.txt · Last modified: 2013/12/24 12:32 by 127.0.0.1