c - C 中的 MapReduce 分区问题

标签 c linux

我目前正在尝试使用结构来保存文件中的键和值的变量。我的字符串变量是一个缓冲区,其中包含 .sample 文件中的随机单词,文件大小是同一文件中的字节总数。

 struct keyValue{
    char* keystring;
    int value;
 };


int main(...){
...
/***** Split the input ****/
      struct keyValue split[numOfWords];
      int x;
      for(x=0; x<numOfWords;x++)
        {

          //split[x].keystring = wordRead(string,filesize);
          split[x].value = 1;
          //printf("this is your output %s\n",split[x].keystring);
        }
....
return 0; 
}

这是我的分割函数:

/**** Read word by word *****/

char * wordRead ( char* strings, unsigned long size){

  static int wordpointer=0;
  char* WordString;
  int i;
  for(i=0; wordpointer < size; wordpointer++,i++) {
    if ((strings[wordpointer] == ' ') )
      {
    //WordString[i+1]='/0';
    wordpointer++;
    break;
      }
    WordString[i]=strings[wordpointer];

  }

  printf("here is your word %s:\n",WordString);
  return WordString;

}

我不断遇到段错误(核心已转储)。我尝试在 for 循环之前调用这些函数几次,它有点工作,但单词重叠,因为 char WordString 是一个指针,前面的字母重叠了 ex。

here is your word hello:
here is your word whylo:

如何转储指针?还有什么方法可以让 split[x].keystring 保存来自 wordRead 函数的字符串?

最佳答案

如果strings的最后一个字符是空格,则您通过递增wordpointer然后请求strings[wordpointer]来读取数组末尾。 我会在分割之前向数据添加一个警告性的trim

关于c - C 中的 MapReduce 分区问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22724600/

相关文章:

C - 包含链表的数组

C 结构--错误 : parameter name omitted

c - C 中的牛顿-拉夫森

c - C 中的数据验证 - 确保输入格式正确

linux - 需要快速中断处理程序

将非常大的数字从二进制转换为十进制并打印出来

c - 应该如何使用c中的字符串数组

c++ - 为什么一个简单的C++程序会产生这么多分支命令?在 Linux 上使用性能

linux - 如何从日期格式的目录列表中复制最新目录

c - 使用 xbuild 在 Linux 上构建 .vcxproj