将文件中的数据读入数组

发布于 2024-10-23 21:56:51 字数 424 浏览 2 评论 0原文

如果我有一个类似于以下内容的选项文件:

size = 4
data = 1100010100110010

并且我有一个 2d size * size 数组,我想将数据中的值填充到其中,那么最好的方法是什么?

为了澄清,对于我的例子,我想要一个像这样的数组:

int[4][4] array = {{1,1,0,0}, {0,1,0,1}, {0, 0,1,1}, {0,0,1,0}}。 (不是真正的代码,但你明白了)。

不过,大小实际上可以是任何数字。

我想我必须读取大小,maloc 一个数组,然后读取一个充满数据的字符串,然后循环遍历数据中的每个字符,将其转换为 int 并将其粘贴在适当的索引中?但我真的不知道如何去做,已经搜索了一段时间但没有运气。

任何帮助都会很酷! :)

If I have an options file along the lines of this:

size = 4
data = 1100010100110010

And I have a 2d size * size array that I want to populate the values in data into, what's the best way of doing it?

To clarify, for the example I have I'd want an array like this:

int[4][4] array = {{1,1,0,0}, {0,1,0,1}, {0,0,1,1}, {0,0,1,0}}. (Not real code but you get the idea).

Size can be really be any number though.

I'm thinking I'd have to read in the size, maloc an array and then maybe read in a string full of data then loop through each char in the data, cast it to an int and stick it in the appropriate index? But I really have no idea how to go about it, have been searching for a while with no luck.

Any help would be cool! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

穿越时光隧道 2024-10-30 21:56:51
int process_file(int **array, char const *file_name)
{
    int size = 0;
    FILE *file = fopen(file_name, "rt");
    if(fp == null)
        return -1;//can't open file
    char line[1024]; //1024 just for example
    if(fgets(line, 1024, file) != 0)
    {
        if(strncmp(line, "size = ", 7) != 0)
        {
            fcloes(file);
            return -2; //incorrect format
        }
        size = atoi(line + 7);
        array = new int * [size];
        for(int i = 0; i < size; ++i)
            array[i] = new int [size];
    }
    else
    {
        fclose(file);
        return -2;//incorrect format
    }
    if(fgets(line, 1024, file) != 0)
    {
        if(strncmp(line, "data = ", 7) != 0)
        {
            fcloes(file);
            for(int i = 0; i < size; ++i)
                delete [] array[i];
            delete [] array;
            return -2; //incorrect format
        }
        for(int i = 7; line[i] != '\n' || line[i] != '\0'; ++i)
            array[(i - 7) / size][(i - 7) % size] = line[i] - '0';
    }
    else
    {
        fclose(file);
        for(int i = 0; i < size; ++i)
            delete [] array[i];
        delete [] array;
        return -2; //incorrect format
    }
    return 0;
}

不要忘记在程序结束前删除数组;

int process_file(int **array, char const *file_name)
{
    int size = 0;
    FILE *file = fopen(file_name, "rt");
    if(fp == null)
        return -1;//can't open file
    char line[1024]; //1024 just for example
    if(fgets(line, 1024, file) != 0)
    {
        if(strncmp(line, "size = ", 7) != 0)
        {
            fcloes(file);
            return -2; //incorrect format
        }
        size = atoi(line + 7);
        array = new int * [size];
        for(int i = 0; i < size; ++i)
            array[i] = new int [size];
    }
    else
    {
        fclose(file);
        return -2;//incorrect format
    }
    if(fgets(line, 1024, file) != 0)
    {
        if(strncmp(line, "data = ", 7) != 0)
        {
            fcloes(file);
            for(int i = 0; i < size; ++i)
                delete [] array[i];
            delete [] array;
            return -2; //incorrect format
        }
        for(int i = 7; line[i] != '\n' || line[i] != '\0'; ++i)
            array[(i - 7) / size][(i - 7) % size] = line[i] - '0';
    }
    else
    {
        fclose(file);
        for(int i = 0; i < size; ++i)
            delete [] array[i];
        delete [] array;
        return -2; //incorrect format
    }
    return 0;
}

Don't forget delete array before program ends;

固执像三岁 2024-10-30 21:56:51

循环。

FILE *fp = fopen("waaa.txt", "r");
if(fp == null) { /* bleh */ return; }

int j = 0;
while(char ch = fgetc(fp)) {
    for(int i = 0; i < 4; ++i) {
        array[j][i] = ch;
    }
    ++j;
}

我不确定 fgetc() 语法。只需检查一下即可。它一次读取一个字符。

Loops.

FILE *fp = fopen("waaa.txt", "r");
if(fp == null) { /* bleh */ return; }

int j = 0;
while(char ch = fgetc(fp)) {
    for(int i = 0; i < 4; ++i) {
        array[j][i] = ch;
    }
    ++j;
}

I am not sure with the fgetc() syntax.. Just check on it. It reads one character at a time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文