Lab 4
You will turn in a log file of your activities.
You have seen printf and scanf. We will use these now in a series of small programs.
First, copy "hello.c" to a new file called "scanf_ex1.c". Then change it to prompt the user to enter an integer value. Next, use scanf to read the value. Remember to use the ampersand before the variable name. After this, print the text "You entered " followed by the integer value, followed by a newline character.
Compile it, and verify that it works.
mweeks@air:csc3320$ gcc scanf_ex1.c -o scanf_ex1
mweeks@air:csc3320$ ./scanf_ex1
Enter an integer value:
23
You entered 23
Next, use "echo" to give it a value, like below. Before you enter this, think about what you expect will happen.
mweeks@air:csc3320$ echo "24" | ./scanf_ex1
Next, what happens if you run the program, and press CTRL-D instead of entering a value? Try it, and see.
Copy the scanf_ex1.c program to a new file, scanf_ex2.c. Edit it to capture the return value from the scanf function call, e.g. my_awesome_return_value = scanf... Have your program print "Scanf returned the value " followed by this value, and a newline character.
Important Note: your programs should produce nice looking output, and the directions from here, and for the rest of the semester, might not explicitly say to add white-space. Unless your program specifically needs to leave white-space out, you should include it.
Compile scanf_ex2.c and verify that it works. Try it a few times with some integer values, and try it at least once with CTRL-D. Here is an example run.
mweeks@air:csc3320$ ./scanf_ex2
Enter an integer value:
3
You entered 3
Scanf returned the value 1
mweeks@air:csc3320$
Copy the scanf_ex2.c program to scanf_ex3.c. Then edit it, to read in a series of integer values. It should print "You entered" and the number after each. Compile it and run it, using CTRL-D to stop the input. Then try the following, and see what you get.
$ echo "2 3 1" | ./scanf_ex3
Next, create a file of at least 10 integer values. Then use "cat" to print them to the screen. After that, repeat the command, and pipe the output to your scanf_ex3 executable program. Verify that it works as expected.
Next, using input redirection to specify the file of integer values to your program. Show that it works as expected.
Copy the scanf_ex3.c program to scanf_ex4.c. Edit it to read in all of the input values, but do not print "You entered" for each one. Instead, print "the sum is" followed by the sum. Verify that it works, and show it working with the file of integer values from Part 3.
CSV stands for comma separated values. You can save a spreadsheet as a CSV file, and then work with it from the command line. As the name implies, a CSV file has a comma after each value. Create a new file with integer values separated by commas, and save it with a filename ending in .csv. You can use the same values as in part 3. Display the contents of your .csv file with cat.
If you try running one of the programs using scanf on the CSV file, it will not work as expected; it will read the same value again and again, likely filling your screen with output. In other words, don't try it here, but feel free to try it when you do are not making a log file to turn in. There are several ways that you can make it work. One is that you can modify the scanf line to include a comma in the format string. Another way, which we will do in this lab, is to edit the input before your program gets it. That is, use sed to read the file and output it without the commas, then pipe the output to scanf_ex3 (yes, use the version from before that prints each "You entered" with each value), and see what it does. Verify that this works as expected. Note that having spaces in the CSV file is not a problem, so "1, 2, 3" works just as well as "1,2,3".
Next, use the same sed command with scanf_ex4, and verify that it computes the sum correctly.
The prompt to "Enter integer values:" is nice when you run the program and type the values yourself, but it is not needed when you provide the input through a pipe or input redirection. Since you know how to tell if the scanf succeeded, you might try doing the scanf first, then printing the prompt if it fails. However, that does not work. Why? Use vi to make a file with your answer, then use cat to display it on the log. (That is, you don't have to do it, just provide an explanation for why it would not work.)