我写了以下代码。我应该用bill改变标签,但是我的代码nothing.What可能是问题所在?我的代码是:
#include <stdio.h>
#include <string.h>
int main ()
{
FILE * pFile;
char tag [6];
char code[20]="bill";
pFile = fopen ("example.asm","r+");
if (pFile==NULL)
{
perror("Error");
}
else
{
while(!feof(pFile))
{
fgets(tag,5,pFile);
if((tag=="<bp>") && (!feof(pFile)))
{
fputs(code,pFile);
}
}
}
fclose(pFile);
return 0;
}发布于 2011-04-10 18:54:52
您不能使用==运算符比较字符串,因为它将比较两个指针之间的字符串,而不是它们所指向的字符串,您应该使用strcmp(tag,"<bp>")。
发布于 2011-04-10 20:30:22
正如所有人在c中所说的那样,比较字符串时使用strncmp或pointers。
#include <stdio.h>
#include <string.h>
int main ()
{
FILE * pFile;
char tag [6];
char code[20]="bill";
pFile = fopen ("example.asm","r+");
if (pFile==NULL)
{
perror("Error");
}
else
{
while(!feof(pFile))
{
fgets(tag,5,pFile);
if((strncmp(tag, "<bp>") == 0) && (!feof(pFile)))
{
fputs(code,pFile);
}
}
}
fclose(pFile);
return 0;
}发布于 2011-04-10 18:54:26
首先,if (tag == "<bp>")不适合C语言。试试strcmp http://www.elook.org/programming/c/strcmp.html
https://stackoverflow.com/questions/5611250
复制相似问题