Reading in strings from a file and storing them in an array as an integer in C

拈花ヽ惹草 提交于 2019-12-11 16:52:59

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/28161286/reading-in-strings-from-a-file-and-storing-them-in-an-array-as-an-integer-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!