倒序输出文件中的文本(英文文本,Linux环境)
/*倒序显示文本内容: linux环境*/ #include<stdio.h> #include<stdlib.h> #define SLEN 81 int main(void) { char *file = "hello.txt"; char ch; FILE *fp; long count, last; if ((fp = fopen(file, "rb")) == NULL) {//只读,fopen 会创建:1. 文件结构体, 2. 缓冲区(这里只建立一个读缓冲区) printf("Error\n"); exit(1); } fseek(fp, 0L, SEEK_END); last = ftell(fp); for (count = 1L; count <= last; count++) { fseek(fp, -count, SEEK_END);//定位到文件末尾向前数count ch = getc(fp); putchar(ch); } putchar('\n'); fclose(fp); return 0; }
posted on 2023-05-04 20:02 david_helloworld 阅读(50) 评论(0) 收藏 举报
浙公网安备 33010602011771号