程序代码:
#include <cstdio> #include <cstring> #include <conio.h> struct Data { double data1; double data2; double data3; }; void main(int argc, char* argv[]) { if (argc < 2) { char programFileName[FILENAME_MAX]; strcpy_s(programFileName, FILENAME_MAX, strrchr(argv[0], '\\') + 1); printf_s("\nUsage: %s <data_filename>\n", programFileName); _getch(); return; } FILE* dataFile; errno_t errorCode; errorCode = fopen_s(&dataFile, argv[1], "rt"); if (errorCode == 0) { Data data; int row = 0; while (!feof(dataFile)) { ++row; int number = fscanf_s(dataFile, "%lf %lf %lf", &data.data1, &data.data2, &data.data3); if ((number != EOF) && (number < 3)) { printf_s("Line: %d read data error\n", row); break; } printf_s("%f %f %f\n", data.data1, data.data2, data.data3); } fclose(dataFile); } else { printf_s("\nFile %s open failure!\n", argv[1]); } _getch(); }
授人以渔,不授人以鱼。