问题描述
我面临与下面的功能的问题。我试图从一个位置获取数据,然后搜索指定的字符串。在那之后我打印结果值。
第一次它的正常工作。如果我使用的是循环调用函数但是我无法打印缓冲区值。
无效parse_data(字符* FNAME,INT标志)
{
焦炭海峡[30] =放大器; LT;响应和放大器; GT;
字符缓冲区[1024],温度[1024],temp1中[1024];
INT NVAL = 0;
FILE * FP;
INT s_pos; //文本字符串位置
INT c_pos; //文本字符位置
字符*串;
焦炭CCNT; //字符计数
长LSIZE;
长POS = 0;
INT℃;
s_pos = -1;
c_pos = 0;
FP = FOPEN(FNAME,R);
// fseek的(FP,1,SEEK_SET);
字符串=的malloc(strlen的(STR)+1);
如果(FP == NULL)
{
的printf(无法打开文件\\ n);
出口(0);
} 而(!的feof(FP))
{
如果(c_pos == 0)
{
为(CCNT = 1; CCNT&下; = strlen的(STR); CCNT ++)
{
如果(!的feof(FP))
{
字符串[CCNT - 1] = GETC(FP);
如果(NVAL == 1)
{
缓冲[POS +] =字符串[CCNT -1];
}
} //如果
}//对于 }//如果
如果(c_pos!= 0)
如果(!的feof(FP))
{
为(CCNT = 0; CCNT&下; = strlen的(STR) - 2; CCNT ++)
字符串[CCNT] =字符串[CCNT + 1];
字符串[strlen的(STR) - 1] = GETC(FP);
如果(NVAL == 1){
缓冲[POS +] =字符串[strlen的(STR) - 1];
}
}
如果(STRCMP(字符串,STR)== 0)
{ 的strcpy(STR,与& LT; /响应和放大器; GT;); s_pos = c_pos;
如果(NVAL == 1){
缓冲[POS-的strlen(STR)] ='\\ 0';
打破;
}
NVAL = 1; }
c_pos ++;
}
如果(FP!= NULL)
FCLOSE(FP);
//的printf(\\ n中的字符串位置为%d = \\ n,s_pos);
如果(标志== 0)
ParsingString_Inserting_To_DataBase(缓冲液);
否则如果(标志== 1)
的printf(缓冲区价值为%s \\ n,缓冲区);
}诠释的main()
{
INT I = 0;
CHAR FNAME [30] =/ tmp目录/ gcc_trans.html
对于(I = 0; I&下; 3;我+ +)
parse_data(FNAME,1);
返回0;
}
我试图理解你的code和失败。一些评论:
1.使用有意义的变量名。尝试模式
而不是 STR
。为 CCNT使用一个更好的名字
, NVAL
-
而不是复杂的循环,试试这个方法:
- 将字符读字符,直到找到一个
&安培;
-
读取N个字节,并检查它们是
LT;响应和放大器; GT;
,其中N是字符串的长度。如果是这样,中断环路。如果不是,寻求倒退N个字节。 -
保存当前的文件偏移量
-
重复code以上
&放大器; LT; /响应和放大器; GT;
。移动此code到一个辅助功能。 -
N =当前偏移 - 第一偏移
- 分配缓冲区与N个字节
- 寻求第一偏移
- 读N个字节到buffer
- 将字符读字符,直到找到一个
I am facing a problem with the function below. I am trying to get data from one location and then search for a specified string. After that I am printing the resulting value.For the first time it's working fine. If I call the function using a for loop however I am unable to print the buffer value.
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;
}
I tried to understand your code and failed. Some comments:
1.Use meaningful variable names. Try pattern
instead of str
. Use a better name for ccnt
, nVal
Instead of the complex loops, try this approach:
- Read character by character until you find a
&
Read N bytes and check whether the they are
lt;Response>
where N is the length of the string. If they are, break the loop. If they are not, seek backwards N bytes.Save the current offset on the file
Repeat the code above with
</Response>
. Move this code into a helper function.N = current offset - first offset
- Allocate buffer with N bytes
- seek to first offset
- read N bytes into buffer
- Read character by character until you find a
这篇关于在C语言中的文件I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!