0

I'm trying to read in a file with a couple hundred integers, some positive, some negative and store them in an array. They have to be read in as a string using strtok, though. I keep getting a segmentation fault and I'm not sure why. The count is to figure out how many total integers are in the file.

/*Input file looks like this:
718321747   -1828022042
-1665405912 -175307986
-53757018 -1551069786 525902369
-1945908378 853648883
*/

int main(int argc, char* argv[])
{
    char buffer[50];
    char* token;
    int count = 0;
    int num = 0;
    int arr[MAX_SIZE];
    if (argc != 2)
    {
        printf("Invalid number of arguments\n");
        return 0;
    }
    FILE* fptr = fopen(argv[1], "r");
    //open file
    if (fptr == NULL)
    {
        printf("Unable to open file\n");
        return 0;
    }
    while(fgets(buffer, 50, fptr))
    //to get the file line by line
    {
        token = strtok(buffer, "\n\t ");
        //find first token
            num = atoi(token);
            //convert it to an int
            arr[count] = num;
            //store in array
            count++;

            while(token != NULL)
            //get rest of tokens and convert to int
            {
                token = strtok(buffer, "\n\t ");
                num = atoi(token);
                arr[count] = num;
                count++;
            }
    }
return 0;
}
2
  • 4
    Shouldn't that 2nd call to strtok() have NULL as the first parameter? Commented Jan 27, 2015 at 0:32
  • Rare for me to see comments after the line of code. Commented Jan 27, 2015 at 1:28

1 Answer 1

2

You never check if the token was found in the string, you must check that strtok() didn't return NULL before trying to call atoi().

Then you keep scanning the same string with strtok() passing the string in each iteration, that's also wrong, you should pass NULL after the first time.

I would also recommend to use strtol() instead of atoi() to check if the conversion was successful.

Check this code, i fixed it

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

#define MAX_SIZE 1000 /* ? whatever value you think is good. */

int main(int argc, char* argv[])
{
    char buffer[50];
    int  count = 0;
    int  arr[MAX_SIZE];

    if (argc != 2)
    {
        printf("Invalid number of arguments\n");
        return 0;
    }

    FILE* fptr = fopen(argv[1], "r");
    //open file
    if (fptr == NULL)
    {
        printf("Unable to open file\n");
        return 0;
    }

    //to get the file line by line
    while ((fgets(buffer, 50, fptr) != NULL) && (count < MAX_SIZE))
    {
        char *pointer;
        char *token;

        pointer = buffer;
        while (((token = strtok(pointer, "\n\t ")) != NULL) && (count < MAX_SIZE))
        {
            char *endptr;

            arr[count] = strtol(token, &endptr, 10);
            printf("%d\n", arr[count]);
            if (*endptr != '\0')
                printf("error: could not convert %s to integer\n", token);
            else
                count++;
            pointer = NULL;
        }
    }
    return 0;
}

I am not sure it will work for you because I haven't seen the structure of your input data, but I am sure it will not cause a segmentation fault.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so now, it doesn't seg fault, but its only reading the first line of the input file. the structure of the data is ints separated by new lines, spaces, and tabs
@BrianHurley You should post a sample of the file's content. For this question, this is the answer and hence you have to post a new question asking why the output is not as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.