#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main() { FILE *fp; char str[81]; int i; if ((fp = fopen("D:\\TEST\\ex10_5.txt", "w")) == NULL) { printf("Open file error!\n"); exit(1); } while (fgets(str, MAX_LINE_LEN, stdin) != NULL) { for (i = 0; str[i] != '\0'; i++) { str[i] = toupper(str[i]); } fputs(str, fp); } fclose(fp); if ((fp = fopen("D:\\TEST\\ex10_5.txt", "r")) == NULL) { printf("Open file error!\n"); exit(1); } while (fgets(str, MAX_LINE_LEN, fp) != NULL) { printf("%s", str); } fclose(fp); return 0; }
时间: 2024-03-07 17:50:32 浏览: 69
这段代码中没有定义`MAX_LINE_LEN`的值,也没有包含头文件,应该将头文件和常量定义加入代码中。以下是修改后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_LINE_LEN 100
int main() {
FILE *fp;
char str[MAX_LINE_LEN];
int i;
if ((fp = fopen("D:\\TEST\\ex10_5.txt", "w")) == NULL) {
printf("Open file error!\n");
exit(1);
}
while (fgets(str, MAX_LINE_LEN, stdin) != NULL) {
for (i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
fputs(str, fp);
}
fclose(fp);
if ((fp = fopen("D:\\TEST\\ex10_5.txt", "r")) == NULL) {
printf("Open file error!\n");
exit(1);
}
while (fgets(str, MAX_LINE_LEN, fp) != NULL) {
printf("%s", str);
}
fclose(fp);
return 0;
}
```
这样,代码中的`MAX_LINE_LEN`常量就被定义为100,并且头文件也被正确包含了。
阅读全文