C:在读取字符串的文件转换成char *所组成的数组 [英] C: Reading in a file of strings into an array of char *s

查看:328
本文介绍了C:在读取字符串的文件转换成char *所组成的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情(不间隔的问题),在那里我读与由符号分隔的字符串的文件,并试图读取每个其中成的char * 组成的数组。然而,每个的char * 持有一个字符串多是字符串的精确长度。这是一个小马车,但。

I have something like this (without the spacing issues), where I'm reading a file with strings that are separated by the # sign, and trying to read each of them into an array of char *s. However, each char* that holds a string much be the exact length of the string. This is a little buggy though.

char * fragments[1000];
 char temp[20];
      while (i < numFrags) {
          if (fscanf(fp, "#%15[^#]#", temp) == 1) {
            char entry[strlen(temp)];
        entry = strcpy(entry, temp);
        fragments[i++] = entry;
        printf("%d\n", strlen(temp));

          }
      }

思考?

推荐答案

您的问题是,你是重用输入阵列。

Your problem is that you are reusing the entry array.

看一下如何为所有的 I 我们片段[我++] =条目; 。最终,每一个片段将指向同一位置,从而将包含相同的字符串(在这种情况下,这将是最后一个字符串读)。

Look at how for all i we have fragments[i++] = entry;. In the end, every fragment will be pointing to the same place and thus will contain the same string (in this case, it will be the last string read).

看来,如果你试图通过同时使用温度输入数组来解决这个问题,但最后你或许也同样作出了的fscanf 直接写输入阵列。

It seems as if you tried to work around this by using both a temp and an entry array, but in the end you might just as well have made the fscanf write directly to the entry array.

这是你能得到的另一个问题是,如果你的指针指向了automaticaly堆栈(即:一个函数内声明的数组)上分配的数组。这些存储单元是由你的程序的函数返回后重复使用,你的指针可以开始指向无效数据(而因为这是C,使用这些损坏的字符串可以导致各种事故和不良程序行为的)。

Another problem that you can get is if your pointers point to an array that was automaticaly allocated on the stack (ie.: an array declared inside a function). These memory locations are reused by your program after your function returns, your pointers can start pointing to invalid data (and since this is C, using these corrupted strings can lead to all sorts of crashes and bad program behaviour).

您的问题基本上是一个内存分配的问题,所以有很多的解决方案。

You problem is basically a memory allocation issue, so there are many solutions.


  1. 片段是字符数组(而不是只指针)的数组。这样,您就放心地为每个字符串不同的内存块。

  1. Make fragments be an array of character arrays (instead of only pointers). This way you safely have a different piece of memory for each string.

char fragments[2000][20]; //can hold 2000 19-character strings
for(i=0; i<N; i++){
    scanf("%d", fragments[i]);
}

该解决方案的优点是,它很简单,并且所有存储器预先分配

The advantage for this solution is that it is simple, and that all memory is allocated beforehand.

使用动态内存分配与的malloc 。这使您可以选择阵列的每个字符串的大小不同,也可以让你在运行时,而不是编译的时候选择你的数组的大小。

Use dynamic memory allocation with malloc. This allows you to choose a different size of array for each string and also allows you to choose the size of your arrays at run time instead of compile time.

char* fragments[2000];
char entry[MAX_FRAGMENT_LENGTH]; //temporary buffer for strings
for(i=0; i<N; i++){
    scanf("%d", entry[i]);
    //dynamically allocate an array, just big enough to fit our string in.
    fragments[i] = malloc(sizeof(char) * (1 + strlen(entry));
    strcpy(fragments[i], entry);
}

有此方法的主要优点是,它是非常灵活的,同时仍然相当简单。主要disavantage是,你将需要免费已被所有的指针的malloc -ed你与他们完成后, (否则你可以得到一个内存泄漏)。

The main advantage for this method is that it is very flexible, while still being reasonably simple. The main disavantage is that you will need to free all pointers that have been malloc-ed after you are done with them (otherwise you can get a memory leak).

用手做动态分配,采用单个大缓冲区来包含所有的字符串。

Do "dynamic allocation" by hand, using a single large buffer to contain all the strings.

char buffer[2000*20];
char* fragments[2000];
char* next_empty_location = buffer;
for(i=0; i<N; i++){
    scanf("%d", next_empty_location);
    fragments[i] = next_empty_location;
    next_empty_location += strlen(next_empty_location) + 1;
}

如果你不能/不想/不允许使用的malloc 解决方案吗,这是最接近什么来。这可能是更难理解(如果您有使用C的问题),但是是最好的似乎适合的模糊的每个字符*持有串多是字符串的精确长度的限制之一。

If you can't/don't want/aren't allowed to use the malloc solution, this is what comes closest. It might be harder to understand (in case you are having issues with C), but is the one that best seems to fit the vague "each char* that holds a string much be the exact length of the string" restriction.

这篇关于C:在读取字符串的文件转换成char *所组成的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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