printf("Running....\n");
rewind(fp);
fread(bute,1,BUTE,fp);
while(1)
{
check(0,BUTE-1);
bute=bute;
err=fread(&bute,1,BUTE-1,fp);
if(!err) break;
} //while
//剩余部分
for(i=BUTE-2;i>0;i--)
if(fread(&bute,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==10)) line++;
if((bute==13) && (bute==10)) line++;
if((bute==10) && (bute==13)) sline++;
if((bute==10) && (bute==13)) sline++;
}//for
}//check() 直接用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); 简练!
不知速度怎样? 因为I/O操作是不断的,所以运行程序就占不少时间,一次READ再运行大量的程序 和 直接getc哪个核算呢? 无法估算
还是实际测量一下为准 测试是从LZ的检测行代码开始和结束.
一个27,427 字节的文件共1021行,实机:TC800,测试5次结果相同.
测试结果:
LZ的代码:13秒
我的代码:12秒~~
看来我的想法是正确的~~ 奇特的现象。但是好象说明不了什么。
或许lz的程序太复杂了,写的罗嗦了些。 偶的代码不仅判断行数,而且判断了空行数。。。
并且判断行的时候是判断0xd,0xa而不是0xd
不知道会不会影响速度?? 当然会影响速度。
不过你的代码比较精确。 呵呵,谢谢lee夸奖 顺便说一下:
windows下换行是0d,0a
linux下换行是0a
若想对于windows和linux都通用,只须统计0a而忽略0d 突然发现gameghost的程序是错的
判断换行应该判断0a 哦!是,我判断的是回车符~~
一个统计txt行数的程序
#define BUTE 100//缓冲区char SAVEADDR[]="/LavaData/ljwfmsg.dat";
char FileDir;
char fname;
char tmpdir;
char data_test={
13,10,13,10,97,13,10,98,13,10
};
char SelectFile();
long line,sline;
char bute;
struct FMSG
{
char filehead;
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==10)) line++;
if((bute==13) && (bute==10)) line++;
if((bute==10) && (bute==13)) sline++;
if((bute==10) && (bute==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('y'!=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=bute;
err=fread(&bute,1,BUTE-1,fp);
if(!err) break;
} //while
//剩余部分
for(i=BUTE-2;i>0;i--)
if(fread(&bute,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=0;
for(;tmpdir!='/';)
tmpdir=0;
ChDir("..");
continue ;
}
if(ChDir(fname))
{
tmp++;
strcat(tmpdir,fname);
strcat(tmpdir,"/");
continue ;
}else tmp--;
strcpy(FileDir,tmpdir);
strcat(FileDir,fname);
return 1;
}
}
//不知道为什么统计不准
//而且算法垃圾,速度太慢
页:
[1]