我们应该从提供的文件中提取字符串,输出符合预期,但它最终报告了分段错误,我不知道为什么。
#include<stdio.h>
#include<string.h>
int main(int argc, char *argv[]){
char str[100];
char f;
int len = 0;
FILE *file;
file = fopen(argv[1],"r");
//read only here, so use "r"
if(file==NULL){
printf("The file doesn't exist.n");
return 1;
}
while(feof(file)==0){
//if feof returns 0 it means it havent reaches the end yet
fread(&f,sizeof(f),1,file);//read in the file
//printabel character between 32 and 126
if(f>=32&&f<=126){
str[len] = f;
len++;
continue;//keep doing it(for ->while)
}
if(strlen(str)>3){
//a string is a run of at least 4
printf("The output is:%sn",str);
len=0;//reset
memset(str, 0, sizeof(str));
//reset the str so it wont get too big(overflow)
}
}
//close the file and return
fclose(file);
return 0;
}
这不是真的
while(feof(file)==0){
//if feof returns 0 it means it havent reaches the end yet
还有一个非常常见的错误。
如果您有 Not read past the end of file
,则返回 0。它微妙但重要的细节。上次读取可能已读取到文件末尾,但未超过文件末尾。这意味着实际上没有数据可以读取,但feof()
仍将返回 0。
这就是您必须测试读取操作结果的原因。
fread(&f,sizeof(f),1,file);
如果返回零,则表示您未能读取任何内容。
这就是为什么你应该构建你的循环来测试读取的结果(而不是feof()
(。
while (fread(&f,sizeof(f),1,file) == 1)
{
// You have successfully read an object from the stream
}
您的代码有一些基本错误:
- 看看为什么
while ( !feof (file) )
总是错的? - 您不检查
fread
是否返回 0,这意味着不再有字符阅读,但您继续使用您的算法 -
str
未' '
端接,则strlen(str)>3
产生未定义第一次迭代中的行为,并且可能会在第一次迭代中被评估为真。然后,出于同样的原因,printf
也会产生未定义的行为。 -
不要直接使用ASCII码,很难阅读,你必须在ASCII 表以查看 32 是什么和 126 是什么。更好地使用字符常数
if(f>= ' ' && f <= '~'){ ... }
这更易于阅读,您可以立即了解代码的意图。
所以程序可以像这样重写:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
char str[100];
char f;
int len = 0;
FILE *file;
file = fopen(argv[1],"r");
//read only here, so use "r"
if(file==NULL){
printf("The file doesn't exist.n");
return 1;
}
memset(str, 0, sizeof str);
while(fread(&f, sizeof f, 1, file) == 1)
{
if(f >= ' ' && f <= '~')
{
str[len++] = f;
continue;
}
if(strlen(str) > 3) // or if(len > 3)
{
printf("The output is: %sn", str);
len = 0;
memset(str, 0, sizeof str);
}
}
fclose(file);
return 0;
}