fscanffstr d 3s d date month year printfdate d month s year dn date month year

Fscanffstr d 3s d date month year printfdate d month

This preview shows page 6 out of 6 pages.

fscanf(fstr, "%d-%3s-%d", &date, month, &year);printf("date = %d, month = %s, year = %d\n", date, month, year); Please note that you should close the file fstr as soon as you have finished using it with fclose(fstr).Convert the above code into a complete program. Create a text file containing that date of format "08-Aug-2006" and test the program using the date file.Get Stuff From String Using sscanfThe following code demonstrates how to extract information from a string with the standard function sscanf:char *date_str = "8-Aug-2006";int date;char month[BUF_SIZE];int year;sscanf(date_str, "%d-%3s-%d", &date, month, &year);printf("Date: %s %d, %d\n", month, date, year);Convert the above code into a complete program and test it with different date strings.Get Command Line ArgumentsWe often run a command with additional arguments from the command line, such asls -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 commandline 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.Lab Assignment:Demonstrate that you can archive all files in a directory and compress the archive file.Demonstrate the program from C Program Exercise 2.Demonstrate the program from C Program Exercise 5.Demonstrate the program from C Program Exercise 8.
Background image

You've reached the end of your free preview.

Want to read all 6 pages?

  • One '14

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture