Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Command-Line Arguments, sscanf()and File Pointers Overview For this weeks lab, you will gain some experience with: command-line arguments using sscanf() to get the values entered

Command-Line Arguments, sscanf()and File Pointers

Overview

For this weeks lab, you will gain some experience with:

command-line arguments

using sscanf() to get the values entered at the command-line

file pointers

working out the logic (i.e. thinking through an algorithm) for what could be a confusing problem

Background Information

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Command-Line Arguments

It is often useful to pass arguments to a program via the command-line. For example,

gcc g -Wall o p12 p12.c

passes 6 arguments to the gcc compiler:

0 gcc (the first one is always the name of the executable)

1 -g

2 -Wall

3 -o

4 p12

5 p12.c

Remember that the main() function header, when using command-line arguments, looks like this:

int main( int argc, char *argv[] )

where argc contains the number of arguments entered at the command-line (including the name of the executable, which is 6 for the above example) and argv[] is the array of pointers, each of which points to the value of the argument that was entered at the command-line. The first item in the argv[] array is always a pointer that points to the name of the executable (gcc in the above example).

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

sscanf()

The sscanf() function is used to extract something that is already in memory. It is often used to get items from the argv[] array when command-line arguments are used. For example, if the second item entered on the command-line was an integer, the following could be used to get that value from argv[1] and store it into an integer variable called num1 (which would have been declared already):

sscanf(argv[1], %d, &num1); // it automatically converts it to an integer and

// stores it in the variable called num1

If the third command-line argument was a string, the following could be used to store that value into a character array that was declared called word:

sscanf(argv[2], %s, word); // no & needed because word is an array

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

File Pointers

Remember that when a program is run, three files are automatically opened by the system and referred to by the file pointers stdin (associated with the keyboard), stdout (associated with the screen), and stderr (also associated with the screen). Users may also create and/or use other files. These files must be explicitly opened in the program before any I/O operations can occur.

A file pointer must be declared and used to access a file. Declaring a file pointer would be in this general form:

FILE *

for example:

FILE * inFile; // for an input file

FILE * outFile; // for an output file

inFile and outFile are just variable names, and as you know, you can name your variables whatever you want.

Once you have a file pointer declared, you can assign it the return value of opening up a file by calling the fopen() function, which might look something like this:

inFile = fopen(inputFileName.txt, r); // r opens it for reading

If the file you are trying to open to read from doesnt exist, the value of the file pointer will be NULL so its a good idea to check and make sure it was successful before continuing in your program.

if (inFile == NULL) {

fprintf(stderr, File open error. Exiting program );

exit(1); // need to #include for this

}

If you are opening a file that you will be writing to, you would use w or a to write or append to a file.

outFile = fopen(outputFileName.txt, w);

If the file doesnt already exist, it will be created.

When getting the filename from a command-line argument, instead of hardcoding the filename in quotes, it will be coming from the argv[] array, so you will have to specify which subscript of the array that it is contained in.

Once the file is opened, you can use file I/O functions to get from or write to the file. Some functions get individual character, some get entire strings, some get entire lines, some get entire blocks of data.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Sum of the Divisors and Perfect/Abundant/Deficient Numbers

Remember that a number that divides evenly into a given number is a divisor. Divisors for 15, for example, would be 1, 3, 5, and 15. The sum of the divisors for 15 therefore would be: 1 + 3 + 5 + 15 = 24. For this program, however, you will not be adding in the given number, so the sum of the divisors of 15 not including 15 would be: 1 + 3 + 5 = 9.

By the way, if the sum of the divisors of a number (not including the number itself) turns out to be the same as the original number, it is said to be perfect. If the sum of the divisors is greater than the original number, it is said to be abundant. If it is less than the original number, it is said to be deficient.

number divisors. sum of divisors

6. 1 2 3. 6 perfect number

15 1 3 5 9 deficient number

20 1 2 4 5 10 22 abundant number

Lab Assignment

For this weeks lab, you will write a program called lab13.c that will print a histogram for a series of numbers beginning with 2, up to a value specified on the command-line. The file will contain a character, which will be used in the histogram. The name of the file will also be specified on the command-line argument.

So, for example, if you create a file called input.txt with the character # in it, and you want the program to print the histogram for each value from 2 through 4 using that character, to run the program you will type:

./a.out input.txt 4

which will produce this output:

2 is Deficient #

3 is Deficient #

4 is Deficient ###

If the file contains the character *, and you run the program like this:

./a.out input.txt 19

then the program will produce this output:

2 is Deficient *

3 is Deficient *

4 is Deficient ***

5 is Deficient *

6 is Perfect ******

7 is Deficient *

8 is Deficient *******

9 is Deficient ****

10 is Deficient ********

11 is Deficient *

12 is Deficient ****************

13 is Deficient *

14 is Deficient **********

15 is Deficient *********

16 is Deficient ***************

17 is Deficient *

18 is Abundant *********************

19 is Deficient *

Notice the line numbers line up as right justified in a column put them in a column width of 3. The deficient/perfect/abundant should be in a column of width -10.

Your program should have both a main() function and a separate function to compute the sum of the divisors for whatever number is sent to it. That function should return the sum of the divisors back to the main() function where it will be used to print out that number of characters. The prototype for that function should look like:

int sumOfDivisors(int aNum);

The logic may take a little bit of effort to work through, so start with a well thought out algorithm, and code the program incrementally. You should start by printing back out to the user the name of the file and the integer value that was entered on the command-line. This involves extracting the file name and the integer value from the argv[] array using sscanf() and storing those values into variables that you can then print to the screen (dont forget to comment out or remove those print statements once you are sure you are extracting those values correctly).

Then you should create an input file containing a character. In your program, open the file, read in the character and then print it to the screen to be sure that it is opening the file and reading the character correctly. As for reading the character from the file, you have several choices of functions to use, such as: fscanf(), fgetc(), fgets().

Next, work on the function for computing the sum of the divisors, and print that value out to the screen. Then add the loop to print that number of characters to the screen.

NOTE:

Remember that the main() function header, when using command-line arguments, looks like this:

int main( int argc, char *argv[] )

where argc contains the number of arguments entered at the command-line (including the name of the executable) and argv[] is the array of pointers, each of which point to the value of the argument that was entered at the command-line.

Using the sscanf() function to retrieve items already in memory is a common way of getting those values from argv[] into a variable.

The following shows an example:

/*

* This program illustrates the use of command-line parameters and sscanf()

* Sample run is: ./a.out John

*/

#include int main(int argc, char * argv[]) {

char name[20];

printf("I have %d arguments ", argc); // will print: I have 2 arguments

printf("Executable is %s ", argv[0]); // will print: Executable is ./a.out

sscanf(argv[1], %s, name); // because name is an array, no & needed

printf("Hi there %s ", name);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago