We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
void cat(int client, FILE *resource) { char buf[1024]; fgets(buf, sizeof(buf), resource); while (!feof(resource)) { send(client, buf, strlen(buf), 0); fgets(buf, sizeof(buf), resource); } }
cat 函数在读取长度小于 1024 的文件时,在第一次 fgets 后 resourse 就已经被设为 eof 了,因此也无法进入循环 send 了。
test.txt
hello world
a.c
#include <stdio.h> #define MAXLEN 20 int main() { FILE *fp; char str[MAXLEN]; fp = fopen("test.txt", "r"); if (fp == NULL) { perror("ERROR OPEN FILE"); return -1; } if (fgets(str, sizeof(str), fp) != NULL) { puts(str); } if (feof(fp)) { puts("eof"); } else { puts("no eof"); } fclose(fp); return 0; }
执行结果
The text was updated successfully, but these errors were encountered:
fix cat EZLippi#31
4c981a4
No branches or pull requests
问题
cat 函数在读取长度小于 1024 的文件时,在第一次 fgets 后 resourse 就已经被设为 eof 了,因此也无法进入循环 send 了。
测试
test.txt
hello world
a.c
执行结果
The text was updated successfully, but these errors were encountered: