C 编程字符串、指针和分配

标签 c string parsing malloc deep-copy

<小时/>

我认为这个问题仅仅是内存分配问题。

(也许跳到底部并阅读最后一个问题以获得一些简单的建议)

我正在编写这个程序来读取用户输入的文件。如果文件“包含”其他文件,那么它们也会被读取。为了检查另一个文件是否包含一个文件,我解析了字符串的第一个单词。为此,我编写了一个返回解析后的单词的函数,并传入一个指针,该指针被设置为下一个单词的第一个字母。例如考虑字符串:

“include foo”注意文件只能包含 1 个其他文件

firstWord == include,chPtr == f

我的算法解析第一个单词以测试字符串与“include”是否相等,然后解析第二个单词以测试文件有效性并查看文件是否已被读取。

现在,我的问题是许多文件正在被读取并且 chPtr 被覆盖。所以,当我将指针返回到下一个单词时。下一个单词有时会包含前一个文件的最后几个字符。考虑名为 testfile-1 和 bogus 的示例文件:

让chPtr原来等于testfile-1,现在考虑'include bogus'的解析:

提取firstWord将==包含,并且chPtr将被覆盖以指向伪造的b。因此,chPtr 将等于 bo g u s '\0' l e - 1。 l e - 1 是 testfile-1 的最后几个字符,因为每次调用我的函数时 chPtr 都指向相同的内存地址。这对我来说是一个问题,因为当我解析 bogus 时,chPtr 将指向 l。这是我的函数的代码:

char* extract_word(char** chPtr, char* line, char parseChar)      
//POST: word is returned as the first n characters read until parseChar occurs in line
//      FCTVAL == a ptr to the next word in line
{
   int i = 0;
   while(line[i] != parseChar && line[i] != '\0')                        
  {
     i++;
  }

  char* temp = Malloc(i + 1);            //I have a malloc wrapper to check validity

  for(int j = 0; j < i; j++)
  {
     temp[j] = line[j];
  }
  temp[i+1] = '\0';

  *chPtr = (line + i + 1);
  char* word = Strdup(temp);             //I have a wrapper for strdup too
  return word;

那么,我的问题诊断正确吗?如果是这样,我是否会制作 chPtr 的深拷贝?另外,如何制作 chPtr 的深拷贝?

非常感谢!

最佳答案

如果我理解正确的话,您想要扫描一个文件,并且当遇到“include”指令时,您想要扫描“include”指令中指定的文件,依此类推,对于任何级别的包含,即读取一个文件,其中可能包含其他文件,而其他文件又可能包含其他文件......

如果是这样(如果我错了,请纠正)那么这是一个经典的递归问题。递归的优点是所有变量都在堆栈上创建,并在堆栈展开时自然释放。

以下代码将执行此操作,无需使用 malloc 或 free 或复制任何内容:

 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>

 #define INCLUDE "include"
 #define INCOFFSET 7

 static void
 process_record (char *name, char *buf)
 {
   // process record here
   printf ("%s:%s\n", name, buf);
 }

 // change this to detect your particular include
 static int
 isinclude (char *buf)
 {
   //printf ("%s:Record %s INCLUDE=%s INCOFFSET=%d\n", __func__, buf, INCLUDE,
 //        INCOFFSET);
   if (!strncmp (buf, INCLUDE, INCOFFSET))
     {
       //printf ("%s:Record == include", __func__);
       return 1;
     }
   return 0;
 }

 static int
 read_file (char *name)
 {

   //printf ("%s:File %s\n", __func__, name);
   FILE *fd = fopen (name, "r");
   if (!fd)
     {
       printf ("%s:Cannot open %s\n", __func__, name);
       return -1;
     }

   char buf[1024];
   ssize_t n;
   while (fgets (buf, sizeof (buf), fd))
     {
       size_t n = strcspn (buf, "\n");
       buf[n] = '\0';
       //printf ("%s:Buf %s\n", __func__, buf);
       if (isinclude (buf))
         {
            read_file (buf + (INCOFFSET + 1));
         }
       else
         {
            process_record (name, buf);
         }
     }
   fclose (fd);

   return 0;
 }

 int
 main (int argc, char *argv[])
 {

   int ret = read_file (argv[1]);
   if (ret < 0)
     {
       exit (EXIT_FAILURE);
     }
   exit (EXIT_SUCCESS);

 }

关于C 编程字符串、指针和分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9450972/

相关文章:

c++ - gcc 用于解析代码

编译所有依赖项并将其链接到 LLVM 位码

c - 提前确定哪个二进制文件将通过 execlp 运行

C:换行检查后在上一行打印

c - 变量的范围。内部工作基础

javascript - 它继承自的字符串的原型(prototype)是什么?

子函数到实际函数的 JavaScript 字符串

python - 如何使用 Pandas 重构简单的数据帧解析代码

xml - jQuery 或 Underscore.js 模板导致 thymeleaf 中的 XML 解析器错误

c - 如何在c中查找字符串在csv文件中的哪一行