0

I have a problem with my program to get it run correctly.

here is the code:

#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;
}

on the output i get some weird output, like realloc overwrites some data... i already tried strcat, but i can't use constant chars. so i have to use realloc.

here is a short excerpt of the output

P 68: {1} (0) /*0*/ |   / open file *
  69: {1} (0) /*0*/ |   FILE* file = fopen(argv[1], "r");
  70: {2} (0) /*0*/ |   if(file == 0) {
rn EXIT_FA(0) /*0*/ |       fprintf(stderr, "error open file '%s'\n", argv[1]);
r open fi (0) /*0*/ |       return EXIT_FAILURE;
  73: {1} (0) /*0*/ |   }
} 74: {1} (0) /*0*/ |

maybe there is another way to reaize this? with fgets() i could get the whole line, but increments the pointer in the file and i have to give fgets() a count of chars. so that wouldn't be the perfect solution.

xlw12
  • 761
  • 1
  • 7
  • 16

1 Answers1

0

You need to terminate your string before you print it out:

    lines++;
    line[col] = 0; // new
    printf("%4d: {%d} (%d) /*%d*/ |%s\n", lines, curlyBraces, braces, comments, line);

Unfortunately line[col] is out of bounds here. So you need realloc line before adding the terminator like this:

        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);

Also, do you know about ungetc? You can replace those fseek(-1) with ungetc.

Charlie Burns
  • 6,994
  • 20
  • 29
  • thanks, i forgot about the termination. but does i really need to reallocate memory for the terminating char? if i initiate the pointer with (char*)malloc(sizeof(char)) would also work? – xlw12 Nov 08 '13 at 18:46
  • The terminating char goes on the end of the line so you need to treat it just like any other character. – Charlie Burns Nov 08 '13 at 18:49
  • If you changed 'realloc(line, (col+1)*sizeof(char));' to 'realloc(line, (col+2)*sizeof(char));' you would haven't to realloc for the final terminator. – Charlie Burns Nov 08 '13 at 18:51