|
发表于 2004-3-20 21:10:04
|
显示全部楼层
到底是\0 还是 \n呢?
写了不行的,请大家指教:rolleyes: :rolleyes:
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define MAXLINE 1
- int
- main(void)
- {
- int fd,n,count ;
- char buf[MAXLINE];
- if((fd=open("abcd",O_RDONLY))<0) {
- printf(" open abcd error\n");
- return(-1);
- }
- while((n=read(fd,buf,MAXLINE))>0)
- if(n=='\0')
- count +=1 ;
- if(n<0) {
- printf("read error\n");
- return(1);
- }
- printf("lines count is %d\n",count);
- return(0);
- }
-
- /forum/c/tmp# cat abcd
- 12323
- 232
-
- /forum/c/tmp# ./a.out
- lines count is 134513734
- /forum/c/tmp#
复制代码
:help :help
这是正确的程序:
- #define MAXLINE 2
- #define RDONLY 00
- int main()
- {
- int fd,n,count=0 ;
- char buf[MAXLINE];
- if((fd=open("abcd",RDONLY))<0) {
- printf(" open abcd error\n");
- return(-1);
- }
- while((n=read(fd,buf,1))>0)
- {
- if(buf[0]==0x0A)
- /* or if(buf[0]=='\0') */
- count +=1 ;
- }
- printf("lines count is %d\n",count);
- close(fd);
- return(0);
- }
复制代码
|
|