行计数和异常结果 [英] Line counting and abberant results

查看:138
本文介绍了行计数和异常结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个实用程序,用于通过Unix命令行对给定文件中的行数进行计数.通常,这对我来说简直太简单了,但显然我晚上要休假.该程序的目标是从命令行中提取未知数量的文件,将它们读入缓冲区并检查换行符.听起来很简单?

I'm writing a utility to count the lines in a given file via the Unix command line. Normally this would be dead simple for me, but apparently I'm having a major off night. The goal of this program is to take in an unknown number of files from the command line, read them into a buffer and check for the newline character. Sounds simple?

int size= 4096;

int main(int argc, char *argv[]){
  int fd, i, j, c, fileLines, totalLines;
  char *buf= (char *)malloc(size); //read buffer

  for (i=2; i<argc; i++){ //get first file

    fileLines=1;    

    if ((fd=open(argv[i], O_RDONLY))!= -1){ //open, read, print file count, close
        while ((c= read(fd, buf, size))!= 0){

            for (j=0; j<size; j++){
                if (buf[j] == '\n')
                    fileLines++;
            }
        }

    }
    printf("%s had %d lines of text\n", argv[i], fileLines);
    totalLines+= fileLines;
    close(fd);

  }

  printf("%d lines were counted overall\n", totalLines);    
  return 0;
}

我有两个问题.首先是第一个printf语句永远不会在调试器之外执行.第二件事是totalLines打印输出应该大约为175K行,但是打印值大约是767倍.

I have two problems. The first is that the first printf statement is never executed outside of the debugger. The second thing is the totalLines printout should be roughly 175K lines, but the printed value is about 767 times larger.

我很难理解这一点,因为所有相关变量都已从其修改声明为超出范围,但这仍然无法解释为什么调试器之外以及调试器之外会忽略第一个打印状态和行计数器更新异常的totalLines结果

I'm having trouble understanding this, because all the relevant variables have been declared out of scope from their modification, but that still doesn't explain why the first print statemeent and line counter update is ignored outside of the debugger along with the abberant totalLines result

感谢您的帮助.

答案

建议进行两项更改.
首先是将j<size更改为j<c.虽然这不是必需的解决方案,但它遵循良好的编码约定

Two changes were suggested.
The first was to change j<size to j<c. While this was not the solution required, it follows good coding convention

第二个是将i=2更改为i=1.我拥有原始启动变量的原因是启动调试器可执行文件的方式.在gdb命令行中,我输入了run lc1 f1.txt以启动调试器.这导致arglist具有三个变量,而且我不知道run f1.txt是否完全合适,因为我的教授通过使用第一个示例向我们介绍了gdb.

The second was to change i=2 to i=1. The reason I had the original start variable was the way I started the debugger executable. In the gdb command line, I entered in run lc1 f1.txt to start the debugger. This resulted in the arglist having three variables, and I didn't know that run f1.txt was perfectly suitable, since my professor introduced us to gdb by using the first example.

推荐答案

考虑:./program file.txt

argv[0] is "program"
argv[1] is "file.txt"

这意味着您的for循环从错误的索引开始,并且如果您仅通过cmd行传递1个文件,则您的代码将永远不会进入该循环!它应该从索引1开始:

which means your for loop starts from the wrong index, and if you are passing only 1 file through the cmd line your code will never enter in that loop! It should start at index 1:

for (i=1; i<argc; i++){

帮自己一个忙,在声明变量时初始化所有变量.这是确保这些内存位置上没有垃圾的唯一方法.

Do yourself a favor and initialize all variables when you declare them. Is the only way to ensure that there will be no garbage on those memory locations.

这篇关于行计数和异常结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆