0
我正在面对下面的函数的一个问题。我试图从一个位置获取数据,然后搜索指定的字符串。之后,我打印结果值。 它第一次工作正常。如果我使用for循环调用函数,但是我无法打印缓冲区值。C语言的文件I/O
void parse_data(char *fname,int flag)
{
char str[30]="<Response>";
char buffer[1024],temp[1024],temp1[1024];
int nVal=0;
FILE *fp;
int s_pos; //string position in the text
int c_pos; //char position in the text
char *string;
char ccnt; //char count
long lSize;
long pos=0;
int c;
s_pos = -1;
c_pos = 0;
fp=fopen(fname,"r");
//fseek(fp, 1, SEEK_SET);
string = malloc(strlen(str)+1);
if(fp==NULL)
{
printf("Unable to open the file \n");
exit(0);
}
while (!feof(fp))
{
if (c_pos == 0)
{
for (ccnt = 1; ccnt <= strlen(str); ccnt++)
{
if (!feof(fp))
{
string[ccnt - 1] = getc(fp);
if(nVal==1)
{
buffer[pos++] = string[ccnt -1];
}
} //if
}//for
}//if
if (c_pos != 0)
if (!feof(fp))
{
for (ccnt = 0; ccnt <= strlen(str) - 2; ccnt++)
string[ccnt] = string[ccnt + 1];
string[strlen(str) - 1] = getc(fp);
if(nVal==1){
buffer[pos++] = string[strlen(str) - 1];
}
}
if (strcmp(string, str) == 0)
{
strcpy(str,"</Response>");
s_pos = c_pos;
if(nVal==1){
buffer[pos-strlen(str)]='\0';
break;
}
nVal=1;
}
c_pos++;
}
if(fp!=NULL)
fclose(fp);
//printf("\n The String position is %d=\n",s_pos);
if(flag==0)
ParsingString_Inserting_To_DataBase(buffer);
else if(flag==1)
printf("The Buffer Value is %s \n",buffer);
}
int main()
{
int i=0;
char fname[30]="/tmp/gcc_trans.html";
for(i=0;i<3;i++)
parse_data(fname,1);
return 0;
}
什么是你想怎么办?为什么用相同的参数做同样的功能3次?它每次打开和关闭同一个文件,所以我们期望3次得到相同的结果,假设文件不会改变.... –