LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: cozo

我想写个给程序统计行数的程序,大家有什么建议?

[复制链接]
发表于 2004-3-25 23:31:41 | 显示全部楼层
wc不更方便吗

另外行数是什么 每一个回车都是一行

如果这样的话

那可以这样写
int count;
char buf[2048];
char *pstart;
FILE* fp=fopen(filename,"rb");//如果是unix就不用b了 win下需要使用
if(!fp)
  exit(1);

while(!eof(fd)){
memset(buf,0,sizeof(buf));
fread(buf,1,sizeof(buf)-1,fp);//读入缓冲区长度-1上字符 因为最后需要保存\0
pstart = buf;
while((pstart=strchr(buf,'\n')) !=NULL){
pstart++;//跳过\n
count++;//以\n结尾的 那是一行 行数++
}
fclose(fp);
发表于 2004-3-25 23:32:29 | 显示全部楼层
同意binary兄的
用perl最方便了

当然更方便的是直接使用wc程序

如果不是为了学习编程的话
发表于 2004-3-25 23:55:17 | 显示全部楼层
主要楼主是要统计PHP,而PHP可以内嵌在HTML之中,这一点就比较难搞,我认为还是找一个perl的语法分析模块或者HTML分析模块比较好,不过找了两篇文章都是介绍性质的,没学会。
要是光数行数的话直接用wc确实是最好的
 楼主| 发表于 2004-3-26 08:37:43 | 显示全部楼层
还有计算函数的个数,类的个数,变量的个数...........
嘿嘿。
发表于 2004-3-26 12:13:15 | 显示全部楼层
最初由 libinary 发表
0x0a就是'\n'
buf[0]是因为只读入了一个字符

  1. while((n=read(fd,buf,1))>0)
  2.     {
  3.         if(buf[0]==0x0A)
复制代码

读入buf的东西不应该是\n结束的么?
那不\n的位置就在n的那个位置了么:ask
发表于 2004-3-26 12:59:20 | 显示全部楼层
Unix 下面有很多有用的工具可以完成这个任务
不提倡从头再写一个
发表于 2004-3-26 20:15:33 | 显示全部楼层
最初由 dancingpig 发表
读入buf的东西不应该是\n结束的么?
那不\n的位置就在n的那个位置了么:ask

read读进来的数据不是'\n'结束的,只有fgets之类的读行的函数是,其它的都不是
发表于 2004-3-28 21:51:50 | 显示全部楼层

  1.        gets() reads a line from stdin into the buffer pointed to  by  s  until
  2.        either  a  terminating newline or EOF, which it replaces with '\0'.  No
  3.        check for buffer overrun is performed (see BUGS below).

  4.        fgets() reads in at most one less than size characters from stream  and
  5.        stores  them  into  the buffer pointed to by s.  Reading stops after an
  6.        EOF or a newline.  If a newline is read, it is stored into the  buffer.
  7.        A '\0' is stored after the last character in the buffer.
复制代码


怪不得get系列的函数都用while(), 而read()不用while(),
发表于 2004-3-29 11:33:41 | 显示全部楼层
最初由 libinary 发表
read读进来的数据不是'\n'结束的,只有fgets之类的读行的函数是,其它的都不是

:ask 那就放在0位干吗?表示这个读好了?
发表于 2004-4-7 18:23:10 | 显示全部楼层
对这里不理解??

  1.     while((n=read(fd,buf,1))>0)
  2.     {
  3.         if(buf[0]==0x0A)
  4. /*   or if(buf[0]=='\0') */
  5.                 count +=1 ;
  6.     }
复制代码


buf[0] 不是一直没有移动吗?都是buf里的第一个字节。?? help
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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