c - 如何将打印到 LOGFILE 的值存储在数组中

标签 c arrays parsing command-line

int parse_command_line (char commandLine[MAX_INPUT_LINE_LENGTH], char* cmds[MAX_CMDS_NUM]){
  char delims[] = "|";
  char *result = NULL;
  result = strtok( commandLine, delims );
 int count = 0;

  if((logfp=fopen("LOGFILE", "w"))==NULL)
    {
      perror("Error");
      exit(1);


  while( result != NULL ) {
    fprintf(logfp, "cmd[%d] will point to string \"%s\"\n", count, result);
    count=count+1;
    result = strtok( NULL, delims );
  }
  fclose(logfp);


  //exit(1);
}

fprintf 语句将行以 delim“|”终止进入日志文件。我希望该值也存储在一个数组中,我可以在接下来的过程中访问该数组。每次向 LOGFILE 添加一行时,我都需要在数组中添加该值。

最佳答案

要做的,只需添加

cmds[count] = result;

fprintf()之后和count=count+1;之前

关于c - 如何将打印到 LOGFILE 的值存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15551261/

相关文章:

c - 尝试将唯一的单词读入文件,但内存分配出现问题

c - 链表中的字符串相互重叠 C

c - 迷你外壳中的 strtok 和 execlp

c++ - 选择排序方法似乎什么都不做

c++ - 输入std::array [duplicate]时遇到麻烦

arrays - 找到两个排序数组的中位数的时间复杂度

c - 我怎么知道要重新分配多少内存?

java - 从 Eclipse 外部以编程方式列出 Eclipse 工作区中的打开项目

使用 NameSpace 解析 XML

file - 从文件中获取数字并放入 Vec<i32> 但不断出现错误