c/c++语言开发共享C语言的文件I / O.

我正面临着下面这个function的问题。 我试图从一个位置获取数据,然后搜索指定的字符串。 之后我打印结果值。 这是第一次工作正常。 如果我使用for循环调用该函数但是我无法打印缓冲区值。

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)]=''; 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; } 

    我试图了解你的代码而失败了。 一些评论:

    1.使用有意义的变量名称。 尝试pattern而不是str 。 为ccntnVal使用更好的名称

      以上就是c/c++开发分享C语言的文件I / O.相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/523073.html

      (0)
      上一篇 2020年12月11日
      下一篇 2020年12月11日

      精彩推荐