c-检查从文件中读取的数据是否等于用户给定的INT值



我正在学习如何对C进行编程,我正在处理一个项目,该项目需要代码从文件中读取值,并将其与用户给定的INT值进行比较。为了处理这个问题,我使用这个代码

int id;
static const char filename[] = "ids.txt";
FILE *file = fopen(filename, "r");
int count = 0;
char line[255];
char line2[255];

if ( file != NULL )
{
char line[256];
while (fgets(line, sizeof line, file) != NULL) /* read a line */
{   
if (id == *line)//PROBLEM IS ON THIS LINE
{
//the rest of my code
}
}      
}

代码运行良好,没有错误,当line的值(读取的数据(和id的值(给定的值(都相同时,程序似乎没有意识到这一点。这行代码if (id == *line)出现了问题。我假设它与变量的数据类型有关,但我似乎找不到解决方法。谢谢你的帮助。

感谢@WeatherVane对我问题的回答。我使用strtol((来解决我的问题,代码现在看起来像这个

int id;
static const char filename[] = "ids.txt";
FILE *file = fopen(filename, "r");
int count = 0;
char line[255];
char *ptr;
long ret;

if ( file != NULL )
{
char line[256]; 
while (fgets(line, sizeof line, file) != NULL) 
{
ret = strtol(line, &ptr, 10);
if (id == ret)
{
//rest of my code
}
}
}   

最新更新