易码技术论坛

 找回密码
 加入易码
搜索
查看: 521962|回复: 15

一个统计txt行数的程序

[复制链接]
发表于 2006-11-26 12:00:00 | 显示全部楼层
用得着这么麻烦么,只要统计换行符的个数
 楼主| 发表于 2006-11-26 14:28:35 | 显示全部楼层
我就是这么写的。
 楼主| 发表于 2006-11-26 14:30:35 | 显示全部楼层
//...................

printf("Running....\n");

rewind(fp);
fread(bute,1,BUTE,fp);
while(1)
{
check(0,BUTE-1);
bute[0]=bute[BUTE-1];
err=fread(&bute[1],1,BUTE-1,fp);
if(!err) break;
} //while
//剩余部分
for(i=BUTE-2;i>0;i--)
if(fread(&bute[1],1,i,fp)) break;
check(0,i+1);
printf("The last:check(%d,%d)\n",0,i+1);
//...................
fclose(fp);
printf("Line:%d\nsLine:%d",line,sline);

while(27!=getchar())continue;
}



void check(int a,int b)
{
int i;
if(b<a)
{
printf("check 入参 err!\n");
getchar();
exit(1);
}
for(i=a;i<=b;i=i+2)
{
if((bute==13) && (bute[i+1]==10)) line++;
if((bute[i+1]==13) && (bute[i+2]==10)) line++;
if((bute==10) && (bute[i+1]==13)) sline++;
if((bute[i+1]==10) && (bute[i+2]==13)) sline++;
}//for
}//check()
发表于 2006-11-26 19:00:49 | 显示全部楼层
直接用getc不就行了,fread一块再在内存搜索换行/回车符理论上确实比getc快,但是在LavaX中始终在操作I/O,所以跟一直读文件没区别~~所以:
long linenum;
char fp;
fp = fopen("文件","r");
for( linenum = 0; !feof(fp);)
{
   if(getc(fp) == 0xD)
   {
      getc(fp);
      linenum++;
   }
}

printf("%d行",linenum + 1);
发表于 2006-11-26 21:28:15 | 显示全部楼层
简练!
不知速度怎样?
发表于 2006-11-26 22:08:58 | 显示全部楼层
因为I/O操作是不断的,所以运行程序就占不少时间,一次READ再运行大量的程序 和 直接getc哪个核算呢?
发表于 2006-11-26 22:12:24 | 显示全部楼层
无法估算
还是实际测量一下为准
发表于 2006-11-27 20:27:40 | 显示全部楼层
测试是从LZ的检测行代码开始和结束.
一个27,427 字节的文件共1021行,实机:TC800,测试5次结果相同.
测试结果:

LZ的代码:13秒

我的代码:12秒~~
看来我的想法是正确的~~
发表于 2006-11-27 22:39:21 | 显示全部楼层
奇特的现象。但是好象说明不了什么。
或许lz的程序太复杂了,写的罗嗦了些。
 楼主| 发表于 2006-12-1 16:25:56 | 显示全部楼层
偶的代码不仅判断行数,而且判断了空行数。。。
并且判断行的时候是判断0xd,0xa而不是0xd

不知道会不会影响速度??
发表于 2006-12-1 17:17:21 | 显示全部楼层
当然会影响速度。
不过你的代码比较精确。
 楼主| 发表于 2006-12-1 17:35:49 | 显示全部楼层
呵呵,谢谢lee夸奖
发表于 2006-12-1 17:39:46 | 显示全部楼层
顺便说一下:
windows下换行是0d,0a
linux下换行是0a
若想对于windows和linux都通用,只须统计0a而忽略0d
发表于 2006-12-1 17:43:46 | 显示全部楼层
突然发现gameghost的程序是错的
判断换行应该判断0a
发表于 2006-12-1 18:57:02 | 显示全部楼层
哦!是,我判断的是回车符~~
 楼主| 发表于 2006-11-25 22:07:43 | 显示全部楼层 |阅读模式
#define BUTE 100  //缓冲区

char SAVEADDR[]="/LavaData/ljwfmsg.dat";
char FileDir[61];
char fname[61];
char tmpdir[61];
char data_test[10]={
13,10,13,10,97,13,10,98,13,10
};

char SelectFile();

long line,sline;
char bute[BUTE];

struct FMSG
{
char filehead[7];
long line_count;
long sline_count;
}fmsg;

int init_data()
{
char fp;
SetScreen(1);
fp=fopen(SAVEADDR,"r");
if(!fp)
{
  printf("Welcome ues the pro!\n");
  printf("For the first run..\n");

  memcpy(fmsg.filehead,"ljwfmsg",7);
  fmsg.line_count=0;
  fmsg.sline_count=0;

  fp=fopen(SAVEADDR,"w");
  if(!fp){printf("New data err!\n");getchar();exit(1);}
  rewind(fp);
  fwrite(fmsg,1,sizeof(struct FMSG),fp);
  fclose(fp);
  printf("Init Data OK!!\n");
  getchar();SetScreen(1);return (2);
}//if not have .dat

line=0;
sline=0;

}

void check(int a,int b)
{
int i;
if(b<a)
{
  printf("check 入参 err!\n");
  getchar();
  exit(1);
}
for(i=a;i<=b;i=i+2)
{
  if((bute==13) && (bute[i+1]==10)) line++;
  if((bute[i+1]==13) && (bute[i+2]==10)) line++;
  if((bute==10) && (bute[i+1]==13)) sline++;
  if((bute[i+1]==10) && (bute[i+2]==13)) sline++;
}//for
}//check()


void pro_test()
{
int i;
SetScreen(1);
printf("测试程序完整性\n");
memcpy(bute,data_test,10);
line=0;sline=0;
// for(i=0;i<=9;i++)
//  printf("%d,",bute);
check(0,9);
// printf("line=%d sline=%d",line,sline);
if(line!=4 || sline!=1)
{
  printf("\ncheck err!!\n");
  getchar();
  exit(0);
}
printf("测试完毕!!\n");

//getchar();
SetScreen(1);
line=0;
sline=0;
}


void main()
{

int key;
int fp;

int err,i;
pro_test();
err=init_data();

SelectFile();
SetScreen(1);
printf("filedir:%s\n",FileDir);

key=getchar();
if(&#39;y&#39;!=key) exit(0);
if(!(fp=fopen(FileDir,"r")))
{
printf("file open err! fp=%d",fp);
getchar();
exit(-1);
}
//...................

printf("Running....\n");

rewind(fp);
fread(bute,1,BUTE,fp);
while(1)
{
check(0,BUTE-1);
bute[0]=bute[BUTE-1];
err=fread(&bute[1],1,BUTE-1,fp);
if(!err) break;
} //while
//剩余部分
for(i=BUTE-2;i>0;i--)
if(fread(&bute[1],1,i,fp)) break;
check(0,i+1);
printf("The last:check(%d,%d)\n",0,i+1);
//...................
fclose(fp);
printf("Line:%d\nsLine:%d",line,sline);

while(27!=getchar())continue;
}

char SelectFile()
{
int tmp;
ChDir("/");
strcpy(tmpdir,"/");
tmp=0;
for(;;)
  {
     if(!FileList(fname) || !strcmp(fname,".."))
     {tmp--;
      if (tmp<0)return 0;
tmpdir[strlen(tmpdir)-1]=0;
for(;tmpdir[strlen(tmpdir)-1]!=&#39;/&#39;;)
tmpdir[strlen(tmpdir)-1]=0;
        ChDir("..");
       continue ;
     }
     if(ChDir(fname))
     {
tmp++;
strcat(tmpdir,fname);
strcat(tmpdir,"/");
continue ;
     }else tmp--;
strcpy(FileDir,tmpdir);
strcat(FileDir,fname);
return 1;
  }
}


//不知道为什么统计不准
//而且算法垃圾,速度太慢
您需要登录后才可以回帖 登录 | 加入易码

本版积分规则

Archiver|手机版|小黑屋|EMAX Studio

GMT+8, 2024-4-25 23:35 , Processed in 0.010881 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表