程序代码:
#include <Windows.h> #include <cstdio> #include <cstdlib> #include <cstring> #include <conio.h> int get_statistics(FILE* file, const int length, const int interval); int main(int argc, char* argv[]) { if (argc < 2) { printf_s("命令行格式: %s <文件名>\n", strrchr(argv[0], '\\') + 1); _getch(); return EXIT_FAILURE; } const char* filename = argv[1]; FILE* file; errno_t error = fopen_s(&file, filename, "rb"); if (error != 0) { printf_s("文件 %s 打開失敗!Error Code = %d\n", filename, GetLastError()); _getch(); return EXIT_FAILURE; } for (int i = 2; i <= 8; ++i) // 模體長度 { for (int j = 1; j <= 3; ++j) // 間隔數 { printf_s("以 %2d 為間隔的 %2d 聨體出現次數 = %d\n", j, i, get_statistics(file, i, j)); } } fclose(file); printf_s("Press any key to continue..."); _getch(); return EXIT_SUCCESS; } int get_statistics(FILE* file,const int length, const int interval) { int number = 0; char* buffer = new char[max(interval, length)]; rewind(file); if (fread(buffer, sizeof(char), length, file) == length) { ++number; while (fread(buffer, sizeof(char), interval, file) == interval) { ++number; } } delete[] buffer; return number; }
[ 本帖最后由 TonyDeng 于 2014-4-22 17:32 编辑 ]
授人以渔,不授人以鱼。