[英]How to read and compare character 255 in C
I need to write a code that searches all the aparition of a string in a file. 我需要编写代码来搜索文件中字符串的所有分区。 Recently, my teacher told me to search string (char)255(char)255 in a file with the same string.
最近,我的老师告诉我在具有相同字符串的文件中搜索字符串(char)255(char)255。 The problem is that I can not read those characters and badly, I cannot distinguish or compare those caracter to EOF;
问题是我看不懂那些字符,而且我无法辨别或比较那些角色与EOF; My code for searching the given string in a file is :
我在文件中搜索给定字符串的代码是:
//problema 14
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char b;
int k=0;
if(argc!=3) {
fprintf(stderr,"Utilizare: %s fisier sir\n",argv[0]);
return 1;
}
if(argv[2][0]=='\0'){
fprintf(stderr, "String vid\n");
return 1;
}
FILE *f;
f=fopen(argv[1],"r");
if (!f)
{
perror(argv[1]);
return 1;
}
int i=0;
while((b = fgetc(f))!=EOF)
{
if(b==argv[2][i]) i++;
else {
fseek(f,-i, SEEK_CUR);
i=0;
}
if(argv[2][i]=='\0'){
k++;
fseek(f,-i+1, SEEK_CUR);
i=0;
}
}
printf("\nSULFUS %d APPEARANCES\n",k);
return EXIT_SUCCESS;
}
what can I do with this code to work on comparint string of (char)255 characters? 我可以用这段代码处理(字符)255个字符的比较字符串吗?
The trick is to realize that fgetc
returns an int
. 诀窍是要意识到
fgetc
返回一个int
。
So, change the data type of b
to int
! 因此,将
b
的数据类型更改为int
!
But then, "while" does nothing, because it seems that 255 is also recognized as EOF or it goes infinite. 但是,“ while”什么也不做,因为似乎255也被认为是EOF或变为无穷大。 I tried also with int but I couldn't figure out much...This teacher is like a compilator, he likes to put you in trouble.
我也尝试过int,但是我想不起来...这位老师就像一个编译器,他喜欢让你陷入困境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.