User Tools

Site Tools


Sidebar

projects

pct0 (bonus; due 20230823)
wcp1 (due 20230823)
abc0 (due 20230830)
pct1 (bonus; due 20230830)
pct2 (due 20230830)
wcp2 (due 20230830)
dac0 (due 20230906)
pct3 (bonus; due 20230906)
wcp3 (due 20230906)
gtf0 (due 20230913)
pct4 (due 20230913)
wcp4 (due 20230913)
pct5 (bonus; due 20230920)
tpb0 (due 20230920)
wcp5 (due 20230920)
gfo0 (due 20230927)
pct6 (due 20230927)
tpb1 (due 20230927)
wcp6 (due 20230927)
pct7 (bonus; due 20231004)
tpb2 (due 20231004)
wcp7 (due 20231004)
bwp1 (bonus; due 20231018)
pct8 (due 20231018)
usr0 (due 20231018)
wcp8 (due 20231018)
fwg0 (due 20231025)
pct9 (bonus; due 20231025)
wcp9 (due 20231025)
gfo1 (due 20231101)
pctA (due 20231101)
upf0 (due 20231101)
wcpA (due 20231101)
pctB (bonus; due 20231108)
usf0 (due 20231108)
wcpB (due 20231108)
ldg0 (due 20231115)
pctC (due 20231115)
wcpC (due 20231115)
bwp2 (bonus; due 20231129)
pctD (bonus; due 20231129)
wcpD (bonus; due 20231129)
gfo2 (due 20231206)
pctE (bonus; due 20231206)
wcpE (bonus; due 20231206)
EoCE (due 20231214)
haas:fall2023: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/fall2023/unix/stdin_redirection.txt · Last modified: 2013/12/24 12:32 by 127.0.0.1