我需要创建一个c程序来从另一个文件的句子中查找“ a”字符。我已经完成了编码,但是仍然没有得到正确的结果。我的编码有什么问题? okeya.txt中的句子是kazega hukeba okeyaga moukaru,但是当我编译程序时,结果是“发现于85”

#include <stdio.h>
#include <stdlib.h>
#define x_size 80
int main( void ){

char filename[] = "okeya.txt"; FILE *fp;
char input[ x_size ];
char find  = 'a';
char *poin;
poin=filename;
char *p = input;

if( (fp = fopen( filename,"r" ) ) == NULL ){ printf( "?????????????" );
exit( -1 );
}

while( fgets( input , x_size , fp ) != NULL );
fclose(fp);

while(*poin!= '\0'){
    if(*poin == find){
        printf("\n found at %d .\n", poin-input + 1);
    }
    poin ++;
}


return 0;
}

最佳答案

您将poin初始化为指向静态字符串filename,而不是所读取文件的内容。因此条件(*poin == find)将在字符串'a'中查找字符"okeya.txt"。所以你应该说

for(poin=input ;*poin; poin++){
    if(*poin == find){
        printf("\n found at %d .\n", poin-input + 1);
        /* here maybe you want to break as well */
    }
}

10-04 22:20