免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3924 | 回复: 6

[C] 请教:这个BUG在哪 [复制链接]

论坛徽章:
0
发表于 2018-01-11 14:16 |显示全部楼层
一个看起来很简单的问题,但程序运行后没有达到要求,不知在哪个地方出现了bug。
问题描述如下:
将文本文件中某特殊字符串前的文本复制写入到输出文件中,其中特殊字符串的位置通过子函数strSearch()查找实现,子函数返回文本文件"strFile"中的第"iNum"个特殊字符串"strTar"出现时的首位置。其代码如下:




if((fp_OutInland=fopen(strOutFileInland,"w+")) == NULL)
        {printf("不能建立结果文件: %s,请检查磁盘空间是否足够!\n",strOutFileInland);
         exit(5);
        }

system("unix2dos.exe  6.fct");
strcpy(strInFile,"6.fct");
lPos21=strSearch(strInFile,"NNNNNNNN",1);
if(lPos21<0)
        {printf("所要查找的字符串[NNNNNNNN [第1次] ]在文件[%s]中不存在.\n",strInFile);
         return -1;
        }
else
         printf("所要查找的字符串[NNNNNNNN [第1次] ]在文件[%s]中位于%ld字节处.\n",strInFile,lPos21);

if((fp_in=fopen(strInFile,"r+")) == NULL)
        {printf("不能打开源文件: %s,请检查该文件是否存在!\n",strInFile);
         exit(4);
        }
for(lTem=0;lTem<lPos21;lTem++)
        {fscanf(fp_in,"%c",&chTmp);
         printf("%c",chTmp);
         fprintf(fp_OutInland,"%c",chTmp);
        }
fprintf(fp_OutInland,"\n              %s年%s月%s日",strYear,strMon,strDay);
fclose(fp_in);
fclose(fp_OutInland);





子函数strSearch()如下:
//////////////////////////////////////////////////
////    子函数strSearch():                    ////
////    返回文本文件中搜索特殊字符串的位置    ////
//////////////////////////////////////////////////
long strSearch(char *strFile, char *strTar, int iNum)
{int  i,j=0,iLen;
long lPos;
char strTmp[256];
FILE *fp_file;

if((fp_file=fopen(strFile,"r+")) == NULL)
        {printf("不能在子函数strSearch中打开目标文件: %s,请检查该文件是否存在!\n",strFile);
         exit(101);
        }
iLen=strlen(strTar);
for(i=0;i<iLen;i++)
         fscanf(fp_file,"%c",&strTmp[i]);
strTmp[iLen]='\0';
lPos=ftell(fp_file);

if(strcmp(strTmp,strTar)==0)
        {lPos=ftell(fp_file)-iLen;
         j++;

         if(j==iNum)
                {fclose(fp_file);
                 return lPos;
                }
        }

while(!feof(fp_file))
        {for(i=0;i<iLen-1;i++)
                strTmp[i]=strTmp[i+1];
         fscanf(fp_file,"%c",&strTmp[iLen-1]);
         if(strcmp(strTmp,strTar)==0)
                {lPos=ftell(fp_file)-iLen;
                 j++;

                 if(j==iNum)
                        {fclose(fp_file);
                         return lPos;
                        }
                }
        }

if(feof(fp_file))
        {fclose(fp_file);
         return -1l;
        }

return -2l;
}



程序能够运行,但没有实现所需要的功能,结果文件中的字符已超过特殊字符串,搞了两天,没有发现bug在哪,请各位大侠指教,不胜感激!

论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
发表于 2018-01-11 16:52 |显示全部楼层
回复 1# titansword2000

rewrite the 子函数strSearch():

write the detail step and information ...

论坛徽章:
0
发表于 2018-01-11 17:02 |显示全部楼层
回复 2# jason680

子函数似乎没问题,因为能顺利找到特殊字符串,且能正确返回特殊字符串的位置。main函数中似乎也没找到问题,所以被这个Bug给搞懵了

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
发表于 2018-01-11 17:10 |显示全部楼层
硬是似乎没看懂
举个栗子

source.txt
  1. This page is part of release 4.14 of the Linux man-pages project.  A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/.
复制代码
output.txt
  1. This page is part of release 4.14 of the Linux man-
复制代码
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char *splitn(char *source, char *target, int nth);

  5. int main() {
  6.     char *source = "source.txt";
  7.     char *output = "output.txt";
  8.     char *match = splitn(source, "page", 2);

  9.     if (match) {
  10.         FILE *file = fopen(output, "w");
  11.         fputs(match, file);
  12.         free(match);
  13.     }
  14. }

  15. char *splitn(char *source, char *target, int nth) {
  16.     FILE *file = fopen(source, "r");
  17.     fseek(file, 0, SEEK_END);
  18.     int size = ftell(file);
  19.     char *buf = calloc(1, size + 1);
  20.     rewind(file);
  21.     fread(buf, size, 1, file);
  22.     fclose(file);

  23.     char *pbuf = buf;
  24.     int len = strlen(target);
  25.    
  26.     for (int i = 0; i < nth; i++) {
  27.         char *find = strstr(pbuf, target);
  28.         if (!find) {
  29.             free(buf);
  30.             return NULL;
  31.         }
  32.         pbuf = find + len;
  33.     }
  34.     *(pbuf - len) = '\0';
  35.     return buf;
  36. }
复制代码


论坛徽章:
0
发表于 2018-01-11 20:31 来自手机 |显示全部楼层
本帖最后由 titansword2000 于 2018-01-11 20:45 编辑
dorodaloo 发表于 2018-01-11 17:10
硬是似乎没看懂
举个栗子


兄台的这个指针用得很不错,佩服。

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
发表于 2018-01-12 14:56 |显示全部楼层
回复 5# titansword2000

似乎没问题,因为能顺利运行
没有发现bug

long lPos21 = strSearch(IN, "page", 1);

所要查找的字符串[page [第1次] ]在文件[s.txt]中位于5字节处.
This

long lPos21 = strSearch(IN, "page", 2);

所要查找的字符串[page [第1次] ]在文件[s.txt]中位于51字节处.
This page is part of release 4.14 of the Linux man-


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. FILE *FOUT;
  5. FILE *fp_in;

  6. long strSearch(char *strFile, char *strTar, int iNum) {
  7.     int i, j = 0, iLen;
  8.     long lPos;
  9.     char strTmp[256];
  10.     FILE *fp_file;

  11.     if ((fp_file = fopen(strFile, "r+")) == NULL) {
  12.         printf(
  13.             "不能在子函数strSearch中打开目标文件: %s,请检查该文件是否存在!\n",
  14.             strFile);
  15.         exit(101);
  16.     }
  17.     iLen = strlen(strTar);
  18.     for (i = 0; i < iLen; i++)
  19.         fscanf(fp_file, "%c", &strTmp[i]);
  20.     strTmp[iLen] = '\0';
  21.     lPos = ftell(fp_file);

  22.     if (strcmp(strTmp, strTar) == 0) {
  23.         lPos = ftell(fp_file) - iLen;
  24.         j++;

  25.         if (j == iNum) {
  26.             fclose(fp_file);
  27.             return lPos;
  28.         }
  29.     }

  30.     while (!feof(fp_file)) {
  31.         for (i = 0; i < iLen - 1; i++)
  32.             strTmp[i] = strTmp[i + 1];
  33.         fscanf(fp_file, "%c", &strTmp[iLen - 1]);
  34.         if (strcmp(strTmp, strTar) == 0) {
  35.             lPos = ftell(fp_file) - iLen;
  36.             j++;

  37.             if (j == iNum) {
  38.                 fclose(fp_file);
  39.                 return lPos;
  40.             }
  41.         }
  42.     }

  43.     if (feof(fp_file)) {
  44.         fclose(fp_file);
  45.         return -1l;
  46.     }

  47.     return -2l;
  48. }

  49. int main() {
  50.     char *OUT = "o.txt";
  51.     char *IN = "s.txt";

  52.     if ((FOUT = fopen(OUT, "w+")) == NULL) {
  53.         printf("不能建立结果文件: %s,请检查磁盘空间是否足够!\n", OUT);
  54.         exit(5);
  55.     }

  56.     // system ("unix2dos.exe  6.fct");
  57.     // strcpy (IN, "6.fct");

  58.     long lPos21 = strSearch(IN, "page", 2);
  59. #define TAR "page"
  60.     if (lPos21 < 0) {
  61.         printf("所要查找的字符串[%s [第1次] ]在文件[%s]中不存在.\n", TAR, IN);
  62.         return -1;
  63.     } else
  64.         printf("所要查找的字符串[%s [第1次] ]在文件[%s]中位于%ld字节处.\n", TAR,
  65.                IN, lPos21);

  66.     if ((fp_in = fopen(IN, "r+")) == NULL) {
  67.         printf("不能打开源文件: %s,请检查该文件是否存在!\n", IN);
  68.         exit(4);
  69.     }
  70.     char chTmp;
  71.     for (long lTem = 0; lTem < lPos21; lTem++) {
  72.         fscanf(fp_in, "%c", &chTmp);
  73.         printf("%c", chTmp);
  74.         fprintf(FOUT, "%c", chTmp);
  75.     }
  76.     // fprintf (FOUT, "\n%s年%s月%s日", strYear, strMon, strDay);
  77.     fclose(fp_in);
  78.     fclose(FOUT);
  79. }
复制代码




论坛徽章:
0
发表于 2018-01-15 08:12 |显示全部楼层
会写程序,应该会调试,一步一步输出日志。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP