读从文件字符串和在阵列中存储它们作为C中的整数 [英] Reading in strings from a file and storing them in an array as an integer in C

查看:125
本文介绍了读从文件字符串和在阵列中存储它们作为C中的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个文件中与一对夫妇读百整数,一些积极,一些消极并将它们存储在数组中。它们必须被读入作为使用的strtok,虽然字符串。我不断收到分段错误,我不知道为什么。伯爵是要弄清楚整数总量有多少的文件中。

  / *输入文件看起来像这样:
718321747 -1828022042
-1665405912 -175307986
-53757018 -1551069786 525902369
-1945908378 853648883
* /INT主(INT ARGC,CHAR *的argv [])
{
    字符缓冲区[50];
    字符*记号。
    诠释计数= 0;
    INT NUM = 0;
    INT ARR [MAX_SIZE];
    如果(的argc!= 2)
    {
        的printf(无效参数数目的\\ n);
        返回0;
    }
    FILE * FPTR =的fopen(的argv [1],R);
    //打开文件
    如果(FPTR == NULL)
    {
        的printf(无法打开文件\\ n);
        返回0;
    }
    而(与fgets(缓冲,50,FPTR))
    //度日行的文件行
    {
        令牌= strtok的(缓冲,\\ n \\ t的);
        //找到第一个标记
            NUM =的atoi(令牌);
            //将其转换为int
            ARR [统计] = NUM​​;
            //店阵列
            算上++;            而(令牌!= NULL)
            //得到令牌的休息和转换为int
            {
                令牌= strtok的(缓冲,\\ n \\ t的);
                NUM =的atoi(令牌);
                ARR [统计] = NUM​​;
                算上++;
            }
    }
返回0;
}


解决方案

您从来没有检查令牌字符串中发现的,你必须检查的strtok()没T返回 NULL 试图调用之前的atoi()

然后你跟上 strtok的扫描相同的字符串()传递字符串在每次迭代中,这也是不对的,你应该通过 NULL 后的第一次。

我也建议使用与strtol()而不是的atoi()来检查,如果转换成功

勾选此code,我固定它

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;#定义MAX_SIZE 1000 / *?你认为什么价值还是不错的。 * /INT主(INT ARGC,CHAR *的argv [])
{
    字符缓冲区[50];
    诠释计数= 0;
    INT ARR [MAX_SIZE];    如果(的argc!= 2)
    {
        的printf(无效参数数目的\\ n);
        返回0;
    }    FILE * FPTR =的fopen(的argv [1],R);
    //打开文件
    如果(FPTR == NULL)
    {
        的printf(无法打开文件\\ n);
        返回0;
    }    //度日行的文件行
    而((与fgets(缓冲,50,FPTR)= NULL)及!及(COUNT< MAX_SIZE))
    {
        字符*指针;
        字符*记号。        指针=缓冲区;
        而(((令牌= strtok的(指针,\\ n \\ t的))= NULL)及!及(COUNT< MAX_SIZE))
        {
            字符* endptr;            ARR [计数] =与strtol(令牌,和放大器; endptr,10);
            的printf(%d个\\ N,编曲[计数]);
            如果(* endptr!='\\ 0')
                的printf(错误:无法转换%s到整数\\ n,令牌);
            其他
                算上++;
            指针= NULL;
        }
    }
    返回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;
}

解决方案

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.

这篇关于读从文件字符串和在阵列中存储它们作为C中的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆