#include // printf() #include // exit() #define PI (3.1415926535897932385L) #define TXTFILE "hw#2a.dat" #define BINFILE "hw#2b.dat" #define TXT_READ #define BIN_READ int main(void) { FILE *fp; float flt_pi; double dbl_pi; long double ldbl_pi; char char_m; int int_m; long long_m; char string[32] = "A two line\nASCII string.\n"; int i; flt_pi = PI; dbl_pi = PI; ldbl_pi = PI; char_m = 100; int_m = 100; long_m = 100; printf("%i ", '\n'); // Writing data to the screen in various ways. printf("Various ways of writing floating point data (some wrong).\n"); printf("%24.20f\n", flt_pi); printf("%24.20lf\n", flt_pi); printf("%24.20Lf\n", flt_pi); printf("%24.20f\n", dbl_pi); printf("%24.20lf\n", dbl_pi); printf("%24.20Lf\n", dbl_pi); printf("%24.20f\n", ldbl_pi); printf("%24.20lf\n", ldbl_pi); printf("%24.20Lf\n", ldbl_pi); printf("Various ways of writing integer data (some wrong).\n"); printf("%c\n", char_m); printf("%i\n", char_m); printf("%li\n", char_m); printf("%c\n", int_m); printf("%i\n", int_m); printf("%li\n", int_m); printf("%c\n", long_m); printf("%i\n", long_m); printf("%li\n", long_m); printf("Various ways of writing character data.\n"); printf("%s", string); for(i = 0; string[i] != '\0'; i++) printf("%c", string[i]); fp = fopen(TXTFILE, "wt"); if(NULL == fp) { printf("Failed to open %s for writing text.\n", TXTFILE); exit(1); } fprintf(fp, "%s", string); for(i = 0; string[i] != '\0'; i++) fprintf(fp, "%c", string[i]); fprintf(fp, "%f\n", dbl_pi); fprintf(fp, "%Lf\n", ldbl_pi); fprintf(fp, "%c\n", int_m); fprintf(fp, "%i\n", int_m); fprintf(fp, "%li\n", long_m); fclose(fp); fp = fopen(BINFILE, "wb"); if(NULL == fp) { printf("Failed to open %s for writing binary.\n", BINFILE); exit(1); } fwrite(string, 1, 32, fp); for(i = 0; string[i] != '\0'; i++) fwrite(&string[i], 1, 1, fp); fwrite(&string[i], 1, 1, fp); fwrite(&flt_pi, sizeof(flt_pi), 1, fp); fwrite(&dbl_pi, sizeof(dbl_pi), 1, fp); fwrite(&ldbl_pi, sizeof(ldbl_pi), 1, fp); fwrite(&char_m, sizeof(char_m), 1, fp); fwrite(&int_m, sizeof(int_m), 1, fp); fwrite(&long_m, sizeof(long_m), 1, fp); fclose(fp); #ifdef TXT_READ // Read the data from the previously written text file fp = fopen(TXTFILE, "rt"); if(NULL == fp) { printf("Failed to open %s for reading text.\n", TXTFILE); exit(1); } printf("\nContents of Text File:\n\n"); // The statements in the following block of code are not in order // Reorganize them so that the screen display exactly matches the file { fgets(string, 40, fp); // fscanf(fp, "%lf\n", &dbl_pi); // fgets(string, 40, fp); // fscanf(fp, "%s", string); // char_m = getc(fp); // printf("%li\n", long_m); // . printf("%s\n", string); // C fscanf(fp, "%s", string); // D printf("%f\n", dbl_pi); // H fgets(string, 40, fp); // S getc(fp); // a int_m = atoi(string); // a fgets(string, 40, fp); // a printf("%s", string); // a fscanf(fp, "%s", string); // a printf("%s ", string); // d printf("%Lf\n", ldbl_pi); // d fscanf(fp, "%li", &long_m); // e printf("%s ", string); // e printf("%s", string); // e printf("%c", '\n'); // o fgets(string, 40, fp); // r ldbl_pi = _atold(string); // r printf("%s", string); // s fgets(string, 40, fp); // t putchar(char_m); // t printf("%i\n", int_m); // v } #endif #ifdef BIN_READ // Read the data from the previously written binary file fp = fopen(BINFILE, "rb"); if(NULL == fp) { printf("Failed to open %s for reading text.\n", TXTFILE); exit(1); } printf("\nContents of Binary File:\n\n"); // Using the byte-by-byte contents of the data file, determine why // the following block of code produces the results that it does. // In other words, execute the statements manually and see if you // get the same results. // // Then reorder the statements so that they behave is they would be // expected to. { i = -1; do { fread(&string[i], 1, 1, fp); i++; } while (string[i] != '\0'); fread(&int_m, sizeof(int_m), 1, fp); printf("%i\n", int_m); printf("%s", string); fread(string, 1, 32, fp); printf("%s", string); fread(&long_m, sizeof(long_m), 1, fp); printf("%li\n", long_m); fread(&char_m, sizeof(char_m), 1, fp); printf("%c\n", char_m); fread(&flt_pi, sizeof(flt_pi), 1, fp); printf("%g\n", flt_pi); fread(&dbl_pi, sizeof(dbl_pi), 1, fp); printf("%g\n", dbl_pi); fread(&ldbl_pi, sizeof(ldbl_pi), 1, fp); printf("%Lg\n", ldbl_pi); } fclose(fp); #endif return(0); }