-1

我正在做一个简单的 UNIX wc命令实现,我遇到了一个非常奇怪的问题。如果我删除第 49 行的 printf,程序将无法正常运行。我尝试了很多东西,但都没有奏效。该程序有效,但输出不是我需要和想要的。

#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include <string.h>
#include<errno.h>
#include<sys/stat.h>

int *counter(FILE* file) {

    // Function counter - counts the number of lines, words and symbols
    int nsymbols = 0, nwords = 0, nlines = 0;
    char c;
    while (EOF != (c = fgetc(file))) {
        nsymbols++;
        if(c == ' '){
            nwords++;
        }
        if(c == '\n'){
            nlines++;
            nwords++;
        }
    }
    int count[] = {nlines, nwords, nsymbols};
    return count;
}

int main(int argc,char* argv[]) {
    if(argc == 1) {
        int *counted;
        counted = counter(stdin);
        printf("\t%d \t%d \t%d\n", counted[0], counted[1], counted[2]);
    }
    else{
        int i,k, bool = 0;
        int total[] = {0,0,0};
        char c = ' ', w = ' ', l = ' ';

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

            // Cheking if there are some options activated
            if(strchr(argv[i], '-')){
                if(strcmp(argv[i], "-")==0){
                    continue;
                }
                if(strchr(argv[i], 'l')) {
                    l = 'l';
                }
                if(strchr(argv[i], 'w')) {
                    w = 'w';
                }
                if(strchr(argv[i], 'c')){
                    c = 'c';
                }
                bool = 1;
            }
        }
        if(!bool) {
            // If none of them are activated from the user, automatically activates all of them
            l = 'l';
            w = 'w';
            c = 'c';
        }
        printf("%c %c %c", l,w,c);

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

            if(strcmp(argv[i], "-") == 0){
                int *counted;
                counted = counter(stdin);
                for(k = 0; k < 3; k++){
                    total[k] += counted[k];
                }
                if(l == 'l')
                    printf("\t%d", counted[0]);
                if(w == 'w')
                    printf("\t%d", counted[1]);
                if(c == 'c') {
                    printf("\t%d", counted[2]);
                }
                printf(" %s\n", "-");
            }
            else{
                if(strchr(argv[i], '-'))
                    continue;
                FILE* file = fopen(argv[i], "r");
                if(file == NULL) {
                    fprintf(stderr, "%s : %s\n", argv[i], strerror(errno));
                    return 1;
                }
                struct stat checker;

                if(stat(argv[i], &checker) < 0 ) {
                    return 2;
                }

                if(checker.st_mode & S_IRUSR) {
                }
                else{
                    fprintf(stderr, "%s : %s\n", argv[i],strerror(EACCES));
                    return 3;
                }

                int *counted;
                counted = counter(file);

                for(k = 0; k < 3; k++){
                    total[k] += counted[k];
                }

                if(l == 'l')
                    printf("\t%d", counted[0]);
                if(w == 'w')
                    printf("\t%d", counted[1]);
                if(c == 'c') {
                    printf("\t%d", counted[2]);
                }
                printf(" %s\n", argv[i]);
            }
        }
        if(l == 'l')
            printf("\t%d", total[0]);
        if(w == 'w')
            printf("\t%d", total[1]);
        if(c == 'c') {
            printf("\t%d", total[2]);
        }
        printf(" total\n");
    }
    return 0;
}
4

4 回答 4

1

它与printf就地一起工作的原因是巧合:您的程序具有未定义的行为,因为您正在返回一个指向本地的指针。分配给局部变量的内存在从函数返回时被重用,因此在函数外部引用该内存会导致访问垃圾值。但是,有时,由于调用者使用堆栈的方式,特定位置不会被重用。在这种情况下,程序看起来可以运行,但即使是对代码的微小更改也可能导致产生不正确的结果甚至崩溃。

更改您的代码如下:

void counter(FILE* file, int count[]) {
    int nsymbols = 0, nwords = 0, nlines = 0;
    char c;
    while (EOF != (c = fgetc(file))) {
         nsymbols++;
         if(c == ' '){
            nwords++;
         }
         if(c == '\n'){
            nlines++;
            nwords++;
         }
      }
      count[0] = nlines;
      count[1] = nwords;
      count[2] = nsymbols;
}

并像这样调用这个函数:

int counted[3];
counter(file, counted);

这将解决问题。

于 2013-03-26T13:21:32.253 回答
0

要么使数组成为静态数组,要么通过 malloc 或 calloc 使用动态内存分配创建它...因为在堆中创建的数组会一直保留到程序结束

于 2013-03-26T14:40:53.497 回答
0

这里的count数组

  int count[] = {nlines, nwords, nsymbols};
  return count;

超出范围,其生命周期在函数返回后结束。您可以将其设为静态或 malloc 内存,这两者都将延长指向的内存的生命周期超出函数的执行时间。

于 2013-03-26T13:21:44.433 回答
0

问题是您返回的数组count是本地数组。你不能那样做。我将是未定义的行为。一切都有可能发生!

数组的大小似乎是固定的,因此您应该声明它并将其作为参数传递给函数 counter()。

于 2013-03-26T13:23:46.007 回答