1. Formatted Output with printf

Last week, we used the printf function to send output to the standard output. This function allows you to construct complex strings consisting of strings, integers, floats, chars, etc, in a very flexible way. For example, the following code constructs the string

Today is 0008 August 2020.

from integer variable day, string variable month and integer year:

int day = 8;
char *month = "August";
int year = 2020;

printf ("Today is %04d %s %d.\n", day, month, year);

In the above printf function, each "%" starts a conversion specification. For example, the first conversion %04d means converting the first variable, day, to a decimal string using at least 4 digits with zero padding. The second conversion %s means convert the second variable, month, to a string. The third conversion, %d, means converting the third variable, year, into a decimal string without padding.

Convert the above code into a complete C program. Run the program to send output to your terminal screen.

Use standard output redirection, so that the program sends its output to a file named foo.

2. Sending Output to a File with fprintf

What if you want to send output to a specific file, such as foo2, rather than to the standard output in your program? Well, you can use another standard function similar to printf : called fprintf. But first you must open file foo2 to get a file stream:

char *fname = "foo2";
FILE *fstr;

fstr = fopen(fname, "w+");
if ( fstr == NULL ) {
fprintf(stderr, "Cannot open file %s, terminate program\n", fname);
exit(1);
}

fprintf(fstr, "bla bla bla ... to %s\n", fname);

In the above code, fopen returns a pointer to FILE if the file is opened for writing successfully by the program code. Otherwise it returns NULL.

If fopen fails, we send an error message

Cannot open file foo2, terminate program

to the standard error. Note that you do not need to open standard error. It is always open automatically.

Finally we can send something to file foo2 using fprintf.

Note printf(.....) is equivalent to fprintf(stdout, .....).

For further details about function fprintf, type the command:

man -s 3 fprintf

Modify the program from Exercise 1 so that the output is sent to file foo2 directly in the program (rather than using standard output redirection).

Please close the file once you have finished using it by

fclose(fstr);

5. scanf Again

Assume the input is in the form of date-month-year, such as 8-August-2006, how would you read the date, the month and the year? Note in this case, the month is not a number. It is a string. See the following code:

int date;
char month[BUF_SIZE];
int year;

printf("Please enter the date in the form of date-short_month-year, ");
printf(" such as 8-Aug-2006, where a month is written in exactly three letters: ");
scanf("%d-%3s-%d", &date, month, &year);

Please note in scanf, we pass the address of array month without using the address operator &. This is because the array name month itself represents the starting address of the array.

Convert the above code into a complete program and test it with date inputs.

8. Get Command Line Arguments

We often run a command with additional arguments from the command line, such as

ls -l -t abc xyz bla

where ls is the command name, or command line argument 0. The strings -l, -t, abc, xyz, and bla are called the 1st, 2nd, 3rd, 4th, and 5th command line arguments, respectively. In the above command line, there are in total 6 command line arguments (including the command name).

How would you get the command line arguments in your progam? One way to get the command line arguments is via argc and argv parameters of the main function, as demonstrated in the following example:

int main(int argc, char *argv[])
{
int i;

printf("There are %d command line arguments.\n", argc);
printf("They are:\n");
for (i=0; i < argc; ++i)
printf("%dth argument: %s\n", i, argv[i]);

exit(0);
}

Note argv[0] points to the command name, argv[1] points to 1st command line argument, etc.

Complete the above program and test it with different number of command line arguments.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.