C 输出中的奇怪行为

C 输出中的奇怪行为,c,realloc,C,Realloc,我的程序无法正常运行 代码如下: #include <stdio.h> #include <stdlib.h> // for EXIT_SUCCESS and EXIT_FAILURE #include <ctype.h> #include <string.h> void ReadFile(FILE* file) { unsigned lines = 0; int braces = 0; int curlyB

我的程序无法正常运行

代码如下:

#include <stdio.h>
#include <stdlib.h>  // for EXIT_SUCCESS and EXIT_FAILURE
#include <ctype.h>
#include <string.h>

void ReadFile(FILE* file) {
    unsigned lines = 0;

    int braces      = 0;
    int curlyBraces = 0;
    int comments    = 0;

    int c;
    char* line = 0;
    unsigned col = 0;

    while ((c = fgetc(file)) != EOF) {
        if(c == '\n') { // new line
            lines++;

            printf("%4d: {%d} (%d) /*%d*/ |%s\n", lines, curlyBraces, braces, comments, line);
            free(line); line = 0;
            col = 0;
        } else {
            // add character to line
            line = (char*)realloc(line, (col+1)*sizeof(char));
            if (line == 0) {
                fprintf(stderr, "error reallocating memory");
                return;
            }
            line[col] = c;
            col++;            

            if (c == '(') {
                braces++;
            } else if (c == ')') {
                braces--;
            } else if (c == '{') {
                curlyBraces++;
            } else if (c == '}') {
                curlyBraces--;
            } else if (c == '/') {
                if (fgetc(file) == '*') {
                    comments++;
                } else {
                    fseek(file, -1, SEEK_CUR);
                }
            } else if (c == '*') {
                if (fgetc(file) == '/') {
                    comments--;
                } else {
                    fseek(file, -1, SEEK_CUR);
                }
            }
        }
    }
}

int main(int argc, char** argv) {
    short lines = 0, words = 0, chars = 0;

    /* check for arguments */
    if (argc == 1) {
        fprintf(stderr, "usage: %s filename\n", argv[0]);
        return EXIT_FAILURE;
    }

    /* open file */
    FILE* file = fopen(argv[1], "r");
    if(file == 0) {
        fprintf(stderr, "error open file '%s'\n", argv[1]);
        return EXIT_FAILURE;
    }

    ReadFile(file);

    if (fclose(file) == EOF) {
        fprintf(stderr, "error in fclose()\n");
        return EXIT_FAILURE;    
    }

    return EXIT_SUCCESS;
}

也许还有其他方法可以实现这一点?使用fgets()我可以得到整行,但是增加文件中的指针,我必须给fgets()一个字符计数。因此,这不是一个完美的解决方案。

在打印字符串之前,您需要终止字符串:

    lines++;
    line[col] = 0; // new
    printf("%4d: {%d} (%d) /*%d*/ |%s\n", lines, curlyBraces, braces, comments, line);
不幸的是,
line[col]
在这里是不允许的。因此,在添加终止符之前,需要
realloc
行,如下所示:

        lines++;
        line = (char*)realloc(line, (col+1)*sizeof(char));
        if (line == 0) {
            fprintf(stderr, "error reallocating memory");
            return;
        }
        line[col] = 0; // new
        printf("%4d: {%d} (%d) /*%d*/ |%s\n", lines, curlyBraces, braces, comments, line);

还有,你知道UNGET吗?您可以将这些fseek(-1)替换为ungetc。

谢谢,我忘记了终止。但是我真的需要为终止字符重新分配内存吗?如果我用(char*)启动指针,malloc(sizeof(char))也可以工作吗?终止字符位于行的末尾,因此您需要像对待任何其他字符一样对待它。如果您更改了“realloc(line,(col+1)*sizeof(char));”至“realloc(行,(列+2)*大小(字符));”你不必为最终的终结者重新定位。
        lines++;
        line = (char*)realloc(line, (col+1)*sizeof(char));
        if (line == 0) {
            fprintf(stderr, "error reallocating memory");
            return;
        }
        line[col] = 0; // new
        printf("%4d: {%d} (%d) /*%d*/ |%s\n", lines, curlyBraces, braces, comments, line);