// lecture_16/example.cc: binary file I/O // #include #include #include // define the max line length // #define MAX_LINE_LENGTH 999 // my main program starts here // int main(int argc, char** argv) { // declare a buffer // char buf[MAX_LINE_LENGTH + 1]; // open the file // FILE* fp = fopen(argv[1], "w"); // write a floating-point number // float sum[2] = {27.25, 27.27272850000001}; fprintf(stdout, "(write) sum = [%f, %f]\n", sum[0], sum[1]); fprintf(stdout, "the number of elements written was %d\n", fwrite(sum, sizeof(float), 2, fp)); // add an integer // short int i = 9; fprintf(stdout, "(write) i = [%d]\n", i); fprintf(stdout, "the number of elements written was %d\n", fwrite(&i, sizeof(short int), 1, fp)); // close the file // fclose(fp); // open the file for reading // FILE* fp2 = fopen(argv[1], "r"); // read two floating-point numbers // float jsum[2]; fprintf(stdout, "the number of elements read was %d\n", fread(jsum, sizeof(float), 2, fp2)); fprintf(stdout, "(read) sum = [%f, %f]\n", jsum[0], jsum[1]); short int ji = 0; fprintf(stdout, "the number of elements read was %d\n", fread(&ji, sizeof(short int), 1, fp2)); fprintf(stdout, "(read) i = [%d]\n", ji); // close the file // fclose(fp2); // open the file for reading and seek past the first four bytes // /* fprintf(stdout, "-----\n"); float sum[2]; sum[0] = -99999; FILE* fp = fopen(argv[1], "r"); int pos = ftell(fp); fprintf(stdout, "start position = %d\n", pos); fseek(fp, sizeof(float), SEEK_SET); fprintf(stdout, "new position = %d\n", ftell(fp)); // read one floating-point number // fprintf(stdout, "the number of elements read was %d\n", fread(sum, sizeof(float), 1, fp)); fprintf(stdout, "(read) sum = [%f]\n", sum[0]); // read another floating-point number // rewind(fp); fprintf(stdout, "the number of elements read was %d\n", fread(&sum[1], sizeof(float), 1, fp)); fprintf(stdout, "(read) sum = [%f]\n", sum[1]); // close the file // fclose(fp); */ // declare a line number counter // /* long nbytes_read = 0; long line_num = 0; FILE* fp = fopen(argv[1], "r"); while (fgets(buf, 999, fp) != (char*)NULL) { // remove the linefeed // buf[strlen(buf) - 1] = (char)NULL; // print the line // fprintf(stdout, "%3d: %s\n", line_num, buf); // convert these strings to numbers // float val1 = -1, val2 = -1, val3 = -1; sscanf(buf, "%f%f%f", &val1, &val2, &val3); fprintf(stdout, "\t=> %f %f %f\n", val1, val2, val3); // increment the line number // line_num++; } */ // loop over the file // FILE* fp = fopen(argv[1], "r"); long num_read = 0; int BUF_SIZE = 4; while ((num_read = fread(buf, sizeof(char), BUF_SIZE, fp)) > 0) { // print the data // fprintf(stdout, "\t=> num_read = %d [%c (%d) %c (%d)]\n", num_read, buf[0], buf[0], buf[1], buf[1]); // clear buf // memset(buf, (int)NULL, 2); } // close the file // fclose(fp); /* for (long i = 0; i < 10; i++) { float val = i; fwrite(&val, sizeof(float), 1, fp); } // loop over the file // long num_read = 0; float newval; while ((num_read = fread(&newval, sizeof(char), 4, fp)) > 0) { // print the data // fprintf(stdout, "\t=> num_read = %d [%f]\n", num_read, newval); } // loop over the file // long num_read = 0; double newval; while ((num_read = fread(&newval, sizeof(char), 8, fp)) > 0) { // print the data // float val1 = *(float*)&newval; float* ptr = (float*)&newval; ptr++; fprintf(stdout, "\t=> num_read = %d [%f %f]\n", num_read, val1, *ptr); } // close the file // fclose(fp); */ // exit gracefully // return 0; }