- 论坛徽章:
- 0
|
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #define SIZE 1000001
- #define NLINE '\n'
- int main(int argc , char *argv[]){
- char buf[SIZE];
- char *p1,*p2,*p3,*p4;
- struct stat *fp;
- int fd;
- fp=(struct stat *)malloc(sizeof(struct stat));
- if(argc != 2){
- fprintf(stderr,"input error %s \n");
- exit(1);
- }
- if( (fd=open(argv[1],O_RDONLY)) == -1 ){
- fprintf(stderr,"open error %s \n",strerror(errno));
- exit(1);
- }
-
- if(fstat(fd,fp)== -1){
- fprintf(stderr,"fstat error %s \n",strerror(errno));
- exit(2);
- }
-
- if(fp->st_size > (SIZE-1)){
- fprintf(stderr,"buffer size is not big enough \n");
- exit(3);
- }
- if(read(fd,buf,fp->st_size) == -1){
- fprintf(stderr,"read error.\n");
- exit(4);
- }
- p1=strchr(buf,NLINE);
- p2=strrchr(buf,NLINE); /*~~~~*/
- *p2='\0'; /*~~~~~*/
- do{
- p2=strrchr(buf,NLINE);
- p4=p2;
- p3=p2+sizeof(char);
- printf("%s\n",p3);
- *p4='\0';
- }while(p2 != p1);
-
- if(p2 == p1){
- *p2 = '\0';
- printf("%s\n",buf);
- }
- return 0;
- }
复制代码 它写的是个简单的tac命令。 波浪线注释的地方有点问题。它把最后一个回车符换为'\0'难道说我们编辑的文本结束时自动加了一个回车符号吗? |
|