【发布时间】:2021-09-22 23:16:36
【问题描述】:
我正在阅读第 11 章(文件处理)中的“C:如何编程”,并使用此算法将字符串附加到名为 info.txt 的文件中,但它不起作用一点也不。我做错了什么?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void)
{
FILE *fp = fopen("info.txt","w");
char buff[100];
if(fp == NULL){
fprintf(stdout,"Error opening file\n");
exit(1);
}
while(!feof(stdin)){
fprintf(stdout,"Type a string/\nEOF ends input\n");
if(!fgets(buff,sizeof buff,stdin)){
fprintf(stderr,"Error reading string");
exit(2);
}
buff[strcspn(buff,"\n")] = 0;
fprintf(fp,"%s",buff);
}
fclose(fp);
}
【问题讨论】:
-
«它根本不起作用»您能更准确地描述一下吗?
-
您的问题描述不是很有帮助。当你运行这个程序时究竟会发生什么? (是没有append的问题吗?因为代码中没有任何东西导致写操作发生在文件末尾。)