我在解决练习时遇到问题。我需要从文件中读取 N 个字符串,但我只能读取第一个。我该如何修复它?
#include <stdio.h>
int main() {
/* variable declarations */
FILE *fp;
char vet[100];
fp = fopen("file.txt","r"); /* open file with N strings */
while(!feof(fp)) {
fgets(vet, 100, fp);
vet[100]='\0';
printf("%s\n", vet);
}
}
最佳答案
vet[100]='\0'
这会在运行时产生错误,而且你也不需要这行代码,因为 fgets 会处理字符串本身的结尾。
关于c - 在没有字符串库的情况下从 C 中的文件读取字符串时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55399403/