c - 使用结构数组从文件读取时出现问题

标签 c

在函数 read_conf 函数中,我从配置文件中读取,当我使用打印函数时,它会根据需要显示结构。

当我调用 Display 函数时,读取时出现问题,因为它仅显示 read 函数中的最后一个条目。

   struct {
     char *ext;
     char *filetype;
    } extensions [10];

void read_conf()
{
 char buf[BUFSIZE];
 char *free1 = NULL;
 char *free2 = NULL;
 FILE *fp;
 fp=fopen("config.conf", "r");
 int i_mime = 0;
 while(fgets(buf,sizeof(buf), fp))
 {
  if(!strncmp(buf,"Listen ",7))
  {
   Port_serv_list = &buf[7];   
   printf("Port number read from conf file is : %s\n", Port_serv_list);
  }  
  if(!strncmp(buf,"DocumentRoot ",13))
  {
   Dir = &buf[13];   
   printf("Directory read from conf file is : %s\n", Dir);
  }  
  if (buf[0] == '.')
  {  
   free1 = strtok(buf," ");   
   extensions[i_mime].ext = &free1[1];

   free2 = strtok(NULL," "); 
   extensions[i_mime].filetype = free2;
   printf("ext: %s\n",extensions[i_mime].ext);
   printf("filetype: %s\n%d\n",extensions[i_mime].filetype, i_mime);
   i_mime++;   
   //printf("Data from conf file \": %s\n",buf);
  }
 }
}

void Display_Content_Conf()
{
 int j_mime = 0;
 printf("Displaying content of Extension Structure array:""\n");
 for(j_mime;j_mime<=7;j_mime++)
 {
  printf("ext: %s\n",extensions[j_mime].ext);
  printf("filetype: %s\n%d\n",extensions[j_mime].filetype, j_mime);
 }
}

输出:

ext: htm
filetype: text/html

0
ext: html
filetype: text/html

1
ext: txt
filetype: text/plain

2
ext: jpeg
filetype: image/jpeg

3
ext: jpg
filetype: image/jpeg

4
ext: png
filetype: image/png

5
ext: gif
filetype: image/gif

6
ext: bmp
filetype: image/bmp

7

显示扩展结构数组的内容:

ext: bmp
filetype: image/bmp

0
ext: bmp
filetype: mage/bmp

1
ext: bmp
filetype: image/bmp

2
ext: bmp
filetype: mage/bmp

3
ext: bmp
filetype: image/bmp

4
ext: bmp
filetype: image/bmp

5
ext: bmp
filetype: image/bmp

6
ext: bmp
filetype: image/bmp

7

最佳答案

看起来 extensions 结构中的指针仍然指向 buf,该指针在每次循环时都会被覆盖(并在退出 时释放) read_conf,因此您看到的任何有效内容都是未定义的行为。

您需要对从 strtok 返回的字符串调用 strdup,将它们放入新分配的动态内存中,以便安全地存储它们。像这样的事情:

free1 = strtok(buf," ");   
extensions[i_mime].ext = strdup(&free1[1]);

free2 = strtok(NULL," "); 
extensions[i_mime].filetype = strdup(free2);

当您不再需要 strdup 返回的字符串时,请记住free() 它们。如果不幸的是您的 C 环境没有提供 strdup 函数(它不在 ANSI C 中,但在 POSIX 中),您可以提供它 - 它是相当简单:

/* Duplicates a supplied string. */
char *strdup(const char *input)
{
    /* Required buffer size is the string length, plus 1 for terminator. */
    size_t size = strlen(input) + 1;

    /* Allocate buffer.  If it fails, return NULL for failure. */
    char *dup = (char *) malloc(size);
    if (dup == NULL) return NULL;

    /* Now copy the string data into the newly allocated buffer and return it. */
    memcpy(dup, input, size);
    return dup;
}

关于c - 使用结构数组从文件读取时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4789268/

相关文章:

c - 如何使用 jsmn 构建 C 脚本?

c - 有问题的 C 宏

c - 如何在 C 中获取函数参数的指针?

创建 n 个 child ,每个 child 都有自己的管道

c - return 从指针目标类型中丢弃 ‘const’ 限定符

c - 如何使用 LD 链接器命令文件将常量放置在特定地址?

C 编程 ncurses 输入验证

c++ - sqlite 通过 c++,打开多个 sqlite 文件

将 void* 转换为 char*

在 C 中将 char 转换为具有特定格式的 uint8_t 数组