#include #include // 实现读取 filepath 文件的前 top_n 行数据 void *read_top_line(const char *filepath, int top_n) { FILE *f = fopen(filepath, "r"); if (NULL == f) { perror("fopen"); return NULL; } char(*lines)[100] = calloc(top_n, 100); for (int i = 0; i < top_n; i++) { char *flag = fgets(lines[i], 100, f); if (flag == NULL) break; } fclose(f); return lines; } int main() { int n = 5; char(*p)[100] = read_top_line("d10.txt", n); for (int i = 0; i < n; i++) { printf("第 %d 行: %s", i + 1, *(p + i)); } free(p); return 0; }