从文件中读取值并将其存储到一个多维数组 [英] Reading values from a file and storing them into a multidimensional array

查看:101
本文介绍了从文件中读取值并将其存储到一个多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的C,所以请让我知道,如果你看到的东西只是明显的愚蠢对我而言:)

I am new to C, so please let me know if you see something just obviously stupid on my part :)

我有了换行分隔,我我想读到我的程序,这样我可以使用它们与其他值来比较两个字符串值的文件。我以为他们会在存储在一个多维数组的最佳方式。我的code看起来是这样的:

I have a file that has newline separated string values that I I'm trying to read into my program so that I can use them to compare with other values. I thought the best way to store them would in in a multidimensional array. My code looks something like this:

char **data;
char *src_line;
int counter = 0; /* To keep track of how many items are in this array */
...
while(fgets(src_line,MAX_LINE_SIZE,fp_src_file) != NULL){
    int tmp = 0;
    while(src_line[tmp] != '\0'){
        if(src_line[tmp] == '\n'){
            src_line[tmp] = '\0';
        }
        tmp++;
    }
    strcpy(data[counter],src_line);
    counter++;
}

这在strcpy的过程中出现segfaults。大部分我在网上发现,多维数组处理资源与常量值这样做。我敢肯定,这可能不是这样做的最佳方式,使..

This segfaults during the strcpy. Most of the resources I've found online that deal with multidimensional arrays do so with constant values. I'm sure that this is probably not the best way to do this so..


  1. 有没有更好的方式来完成我想要做什么?

  2. 什么是获得一个未知数量的项目成为一个多维数组的正确方法。应我甚至可以这样做?

谢谢!

推荐答案

正如我上面的评论,这不是我清楚,如果你不正确的数据分配内存,或者如果你不是在所有分配吧:)数据是char **。你希望它是字符数组的数组,所以你需要(一)的char * S的顶层数组分配内存,然后(二)字符本身的数组分配内存。

As I commented above, it's not clear to me if you're not allocating memory correctly for data, or if you're not allocating it at all :) data is a char**. You want it to be an array of character arrays, so you need to (a) allocate memory for the top-level array of char*s, and then (b) allocate memory for the arrays of chars themselves.

所以,如果你有一个未知数量的项目的最简单的方法就是拿出一个合理的最大值和该最大值分配足够的,或者您也可以通过文件曾经只是找\\纳秒读取,算上这些,然后数据适当地分配内存,等等。或者你可以开始了与有关你要多少行有一个假设,相应地分配数据,然后如果/当你运行的空间创造双倍大小的新数组复制所做的一切。这听起来好像比你想要更多的工作。

So if you have an unknown number of items the easiest solution is just to come up with a reasonable max and allocate enough for that max, or you can read through the file once just looking for \ns, count those, and then allocate memory for data appropriately, etc. Or you could start out with an assumption about how many lines you're going to have, allocate data accordingly, and then if/when you run out of space create a new array of double the size and copy everything over. That sounds like more work than you want.

另一种方法可能是简单的 - FSTAT整个文件,只是创造一个缓冲区大到足以容纳整个事情。阅读整个文件。通过迭代,切换\\ NS为\\ 0,当你去计算行。然后创建一个char **尺寸==行数你看到了,并将其分配给每个行的开始。瞧 - 你有每行一个字符串,你没有做尽可能多的内存分配碴左右。

Another approach that might be simpler -- fstat the whole file, and just create one buffer big enough to hold the whole thing. Read the whole file in. Iterate through, switch the \ns for \0s and count lines as you go. Then create a char** of size == the number of lines you saw and assign them to the start of each line. Voila -- you have a string per line and you didn't have to do as much mucking around with memory allocation.

这篇关于从文件中读取值并将其存储到一个多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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