LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 724|回复: 10

为什么出错时没有出现程序里的语句?

[复制链接]
发表于 2004-4-3 15:58:41 | 显示全部楼层 |阅读模式
example:

  1. if( argc != 3) {
  2.       printf(" arg error\n");
  3.       exit(1);
  4. }
复制代码


如果运行是不输入3个参数就会出现:
Segmentation fault

为什么,怎么使它有原来的:arg error ??
发表于 2004-4-3 16:13:59 | 显示全部楼层
从你提供的代码看不出来有问题,是不是这段代码前面还有其它的代码?
 楼主| 发表于 2004-4-3 16:48:41 | 显示全部楼层
谢谢,全部的代码如下:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <fcntl.h>
  4. int
  5. main(int argc,char **argv)
  6. {
  7.      unsigned   n=atol(argv[3]),count_line=atol(argv[2]);
  8.      unsigned   i;
  9.      FILE  *fd;
  10.      char  buffer[8192];
  11.      if( argc != 4 ) {
  12.            printf("Usage : %s <file> <count_line> <byte>\n",argv[0]);
  13.            return(1);
  14.      }
  15.      if((fd=fopen(argv[1],"r"))<0) {
  16.           printf("open error\n");
  17.           return(1);
  18.      }
  19.      for(i=0;i<count_line;i++) {
  20.           if (fgets(buffer,8192,fd) != NULL )
  21.                if(write(1,buffer,n) != n) {
  22.                     printf("write error\n");
  23.                     return(1);
  24.                }
  25.           printf("\n");
  26.      }
  27.      fclose(fd);
  28.      return(0);
  29. }
  30. # ./a.out /etc/fstab
  31. Segmentation fault

复制代码


是不是atol()这个函数的问题?:confused:
发表于 2004-4-3 16:53:40 | 显示全部楼层
n=atol(argv[3]),

如果不输入3个参数,上面这行代码就会导致Segmentation fault
 楼主| 发表于 2004-4-3 16:56:46 | 显示全部楼层
还有个从后面数上的行数,从这些行中读取n个字节的程序,看不出错在哪里??

  1. int
  2. main(int argc,char **argv)
  3. {
  4.      unsigned   n=atol(argv[3]),count_line=atol(argv[2]);
  5.      unsigned   i=0,line_tital=0;
  6.      FILE  *fd;
  7.      char  buffer[8192];
  8.       if( argc != 4 ) {
  9.            printf("Usage : %s <file> <count_line> <byte>\n",argv[0]);
  10.            return(1);
  11.      }
  12.      if((fd=fopen(argv[1],"r"))<0) {
  13.           printf("open error\n");
  14.           return(1);
  15.      }
  16.      while(fgets(buffer,8192,fd) != NULL )
  17.              line_tital++ ;
  18.       count_line = line_tital - count_line;
  19.       for(i=0;i<line_tital;i++) {
  20.              if(fgets(buffer,8192,fd) != NULL )
  21.                      if( i > count_line)
  22.                            if(write(1,buffer,n) != n) {
  23.                                   printf("write error\n");
  24.                                   return(1);
  25.                            }
  26.      }
  27.      printf("%d\n",line_tital);
  28.      fclose(fd);
  29.      return(0);
  30. }
复制代码
发表于 2004-4-3 20:53:39 | 显示全部楼层

  1. while(fgets(buffer,8192,fd) != NULL )
  2.   line_tital++ ;
复制代码

这样还是有问题,如果一行比8192长的话就会多计算一行。

你的主要问题是while以后文件读写位置已经到了文件末尾,这时候再fgets是读不到数据的。
你能不能把你的程序要实现的功能描述一下。
 楼主| 发表于 2004-4-3 20:59:19 | 显示全部楼层
最初由 libinary 发表

  1. while(fgets(buffer,8192,fd) != NULL )
  2.   line_tital++ ;
复制代码

这样还是有问题,如果一行比8192长的话就会多计算一行。

你的主要问题是while以后文件读写位置已经到了文件末尾,这时候再fgets是读不到数据的。
你能不能把你的程序要实现的功能描述一下。



  1. while(fgets(buffer,8192,fd) != NULL )
  2.   line_tital++ ;
复制代码
的作用是计算这个文件有多少行。

这个程序的目的是读取一行中的从开始的n字节在从后面数起的一些行。

example:
a file :
a1234567
b1234567
c1234567
d1234567
e1234567

#./a.out file 2 5
d1234
e1234
 楼主| 发表于 2004-4-4 13:47:05 | 显示全部楼层
帮忙阿!!
发表于 2004-4-4 17:53:57 | 显示全部楼层
写了一个,错误处理都没加,可能会有问题
程序是把文件一次读进file,用lines指向最后几行,用n控制输出字符数

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>

  7. int
  8. main(int argc, char *argv[])
  9. {
  10.   int l, n;
  11.   char **lines;
  12.   int fd;
  13.   struct stat st;
  14.   char *file;
  15.   int i;

  16.   if(argc != 4){
  17.     fprintf(stderr, "Usage: %s <file> <count_line> <byte>\n", argv[0]);
  18.     exit(-1);
  19.   }
  20.   l = atoi(argv[2]);
  21.   n = atoi(argv[3]);
  22.   if(l == 0 || n == 0){
  23.     fprintf(stderr, "count_line or byte is zero\n");
  24.     exit(-1);
  25.   }
  26.   
  27.   if((fd = open(argv[1], O_RDONLY)) < 0){
  28.     fprintf(stderr, "Cannot open %s\n", argv[1]);
  29.     exit(-1);
  30.   }
  31.   fstat(fd, &st);
  32.   file = (char *)malloc(st.st_size + 1);
  33.   lines = (char **)malloc(l * sizeof(char *));
  34.   read(fd, file, st.st_size);
  35.   file[st.st_size] = '\0';
  36.   if(file[st.st_size - 1] == '\n')
  37.     file[st.st_size - 1] = '\0';
  38.   for(i = l - 1; i >= 0; i--){
  39.     if((lines[i] = strrchr(file, '\n')) == NULL){
  40.       lines[i] = file;
  41.       break;
  42.     }
  43.     *lines[i] = '\0';
  44.     lines[i]++;
  45.   }
  46.   if(i == -1)
  47.     i = 0;
  48.   for(;i < l; i++){
  49.     if(strlen(lines[i]) < n)
  50.       puts(lines[i]);
  51.     else{
  52.       write(STDOUT_FILENO, lines[i], n);
  53.       printf("\n");
  54.     }
  55.   }

  56.   close(fd);
  57.   free(file);
  58.   free(lines);
  59.   exit(0);
  60. }
复制代码
发表于 2004-4-4 18:02:50 | 显示全部楼层
其实,如果不是学习编程的话,这个东西用不着编程实现,用命令
tail -2 ts | cut -c 1-5
就可以看ts文件的最后2行的1-5个字符
你也可以写个shell脚本
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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