c - 如何在C中正确分割?

标签 c arrays split

我想拆分一个包含文本文件中的信息的数组。首先,我拆分然后插入一个节点。我写了一些东西,但仍然有错误。你能帮我吗?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct person
{
  char *lesson;
  char *name;
  struct person *next;
};

char *
strtok_r (char *str, const char *delim, char **save)
{
  char *res, *last;

  if (!save)
    return strtok (str, delim);
  if (!str && !(str = *save))
    return NULL;
  last = str + strlen (str);
  if ((*save = res = strtok (str, delim)))
    {
      *save += strlen (res);
      if (*save < last)
        (*save)++;
      else
        *save = NULL;
    }
  return res;
}


int
main ()
{

  FILE *fp;
  char names[100][100];
  fp = fopen ("C:\\lesson.txt", "r");

  int i = 0, j = 0;
  while (!feof (fp))
    {
      //fscanf(fp,"%s",names[i]);
      fgets (names[i], sizeof (names), fp);
      i++;
    }
  for (j = 0; j < 10; j++)
    {
      char *key_value;
      char *key_value_s;

      key_value = strtok_r (names[j], ":", &key_value_s);

      while (key_value)
        {
          char *key, *value, *s;

          key = strtok_r (key_value, ":", &s);
          value = strtok_r (NULL, ",", &s);

          key_value = strtok_r (NULL, ",", &key_value_s);
          insertion (key_value);
        }
    }
}

这是我的 Lesson.txt 文件:

George Adam            :Math,Science,Germany
Elizabeth McCurry      :Music,Math,History
Tom Hans               :Science,Music

我想要拆分名称和类(class),并且想要插入类(class)。但我只能插入名字。我使用 strtok_r 但我认为它不能正常工作。因为我确信我的插入函数是正确的。我想帮忙分割代币。

我的输出是这样的:

Elizabeth McCurry, George Adam, Tom Hans

但我想要这样的输出:

Germany, History, Math, Music, Science

最佳答案

好的,这里是如何执行此操作的示例。我正在使用您的类(class)文件(我将其重命名为 temp.txt),并且我也在 Linux 上使用 Ubuntu 上运行的 gcc 版本 4.8.2 执行此操作。我还删除了 strtok_r 因为 strtok_rstrtok 的线程安全版本,并且因为您没有使用线程,所以我认为没有理由使用可重入版本。最后我添加了一点,但只是一点,错误检查。代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main(int argc, char** argv)
{
  FILE*  fp = NULL;
  int    ndx = 0;
  char   lines[100][100];

  if(NULL != (fp = fopen("./temp.txt", "r")))
  {
    while(!feof(fp))
    {
      fgets(lines[ndx++], sizeof(lines), fp);
    }

    for(ndx = 0; ndx < 3; ndx++)
    {
      char*      name;
      char*      list;

      name = strtok(lines[ndx], ":");
      list = strtok(NULL, ":");

      printf("Name: %s  List: %s", name, list);
    }
  }
  else
  {
    printf("Unable to open temp.txt, error is %d\n", errno);
  }

  return 0;
}

在我的计算机上运行示例:

******@ubuntu:~/junk$ gcc -ansi -Wall -pedantic array.c -o array
******@ubuntu:~/junk$ ./array
Name: George Adam              List: Math,Science,Germany
Name: Elizabeth McCurry        List: Music,Math,History
Name: Tom Hans                 List: Science,Music
******@ubuntu:~/junk$ 

希望这足以帮助您入门。如果没有,请随时提出另一个问题或修改这个问题。我看到您使用的是 Windows,但上面的代码应该足够通用,可以在其中进行编译,只是请注意,您可能需要添加一两个包含文件。

关于c - 如何在C中正确分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27470273/

相关文章:

Python。使用单词列表中的任何单词拆分字符串

c - 将指针/数组从函数传递给 main()

c - 用 C 符号对文件进行编码

c - 如何将中缀表达式转换为后缀表达式?

c - 为什么 semop(semid,&wait[1],2);在一组三个信号量上减少第 0 个信号量而不是第 1 个和第 2 个信号量的值?

java - 如何将元素添加到数组中的下一个可用空间?

java - 将 int 数组插入到 ArrayList

javascript - file_put_content 数组结构不起作用

Java 拆分和替换字符串

javascript split() 函数 - 每个字符串末尾的幽灵字符