If fopen fails we send an error message Cannot open file foo2 terminate program

If fopen fails we send an error message cannot open

This preview shows page 4 - 6 out of 6 pages.

If fopen fails, we send an error messageCannot open file foo2, terminate programto 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 fprintfModify 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 byfclose(fstr);Generate Formatted String with sprintfSometimes we want to contruct a complex string to be used within the program rather than to send to the user. This can be done using another standard function similar to printf and fprintf called sprintf. For example, the following code sends the constructed string to the char array long_date:#define BUF_SIZE 256int day = 8;char *month = "August";int year = 2006;
Background image
char long_date[BUF_SIZE];sprintf (long_date, "Today is %04d %s %d.\n", day, month, year);Modify Exercise 1 to send the constructed string to a char array. Then print thearray to the standard output.Get Input From Standard Input with scanfAssume we want to receive user input of date, month and year in the form of dd/mm/yyyy such as 08/08/2006, is there an easy way to get the date, month and year into three separate variables? The standard function scanf can help. The following code prompts the user to enter the date in the form of "dd/mm/yyyy" and reads the date, the month, and the year into variables date, month and year:int date, month, year;printf("Please enter the date in the form of dd/mm/yyyy: ");scanf("%d/%d/%d", &date, &month, &year);printf("the date you entered was: %d-%d-%d\n", date, month, year);Note you must pass the addresses of variables date (ie, &date), month (ie, &month) and year (ie, &year), not their values, to function scanf.Convert the above code into a complete program. Test the program with some keyboard input of dates.scanf AgainAssume 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 isnot 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.Get Input from a File with fscanfAs with printf and fprintf, there is also a function similar to scanf that allows you to get input directly from a file rather than just from the standard input. It is called fscanf. See the following code:char *fname="foo3";FILE *fstr;int date;char month[BUF_SIZE];int year;fstr = fopen(fname, "r");if (fstr == NULL) {fprintf(stderr, "Cannot open file %s, exit.\n", fname);exit(1);}
Background image
Image of page 6

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