Why am I getting a segmentation error?

I am trying to write a program that takes a plaintext file as an argument and parses it, adding all the numbers together and then printing the sum. Below is my code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

static int sumNumbers(char filename[])
{
    int sum = 0;
    FILE *file = fopen(filename, "r");
    char *str;

    while (fgets(str, sizeof BUFSIZ, file))
    {
        while (*str != '\0')
        {
            if (isdigit(*str))
            {
                sum += atoi(str);
                str++;
                while (isdigit(*str))
                    str++;
                continue;
            }
            str++;
        }
    }

    fclose(file);

    return sum;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Please enter the filename as the argument.\n");
        exit(EXIT_FAILURE);
    }
    else
    {
        printf("The sum of all the numbers in the file is : %d\n", sumNumbers(argv[1]));
        exit(EXIT_SUCCESS);
    }

    return 0;
}

And the text file that I use:

This is a pretty boring text file with some random numbers scattered all over.

Here is one: 87, and here is another: 3

and finally, the last two numbers: 12 19381. Done. Phew

When I compile and try to run it, I get a segmentation error.

+3
source share
8 answers

.
str - . , , , , , .

:

char *str;
str = malloc(BUFSIZ); // this is missing..also free() the mem once done using it.

:

char str[BUFSIZ]; // but then you can't do str++, you'll have to use another 
                  // pointer say char *ptr = str; and use it in place of str.

EDIT:

:

while (fgets(str, sizeof BUFSIZ, file))

BUFSIZ not sizeof BUFSIZ.

?

- , , . sizeof BUFSIZ - 4, max 3 char . , 19381 193, 81<space>.

+14

- str. fgets , .

char *str; , char str[BUFSIZ];

+3

.

+2

, , . , :

        if (isdigit(*str))
        {
            if (isdigit(*str))
            {
                sum += atoi(str);
                str++;
                while (isdigit(*str))
                    str++;
                continue;
            }
        }

if ? ( : else).

+2

char * str, . malloc.

, , valgrind. .

+1
char *str;

str . malloc(), , .

char str[MAX_SIZE];
+1

:

  • . , , . , 4, The |numb|er 1|2345| is |larg|e., . 1 2345 .
  • isdigit char. - "" (, SCHAR_MAX), undefined. , . , unsigned char, isdigit((unsigned char) *str). , , fgetc, , , isdigit.
  • , (fgets), . , - char buffer[BUFSIZ].
  • str : ( ) ( ). . buffer p ( ).

:

#include <ctype.h>
#include <stdio.h>

static int sumNumbers(const char *filename)
{
    int sum, num, c;
    FILE *f;

    if ((f = fopen(filename, "r")) == NULL) {
        /* TODO: insert error handling here. */
    }

    sum = 0;
    num = 0;
    while ((c = fgetc(f)) != EOF) {
        if (isdigit(c)) {
            num = 10 * num + (c - '0');
        } else if (num != 0) {
            sum += num;
            num = 0;
        }
    }

    if (fclose(f) != 0) {
        /* TODO: insert error handling here. */
    }

    return sum;
}

int main(int argc, char **argv) {
    int i;

    for (i = 1; i < argc; i++)
        printf("%d\t%s\n", sumNumbers(argv[i]), argv[i]);
    return 0;
}
+1

, :

static int sumNumbers(char* filename) {
    int sum = 0;
    FILE *file = fopen(filename, "r");
    char buf[BUFSIZ], *str;

    while (fgets(buf, BUFSIZ, file))
    {
            str=buf;
            while (*str)
            {
                    if (isdigit(*str))
                    {
                            sum += strtol(str, &str, 10);
                    }
                    str++;
            }
    }
    fclose(file);
    return sum;
}

, .

: 19483

0

Source: https://habr.com/ru/post/1768755/


All Articles