简体   繁体   中英

Problem using struct array for reading from file

In function read_conf function, I read from the config file and when I use the print function it displays the the structs as desired.

When I call the Display function, then there is problem reading as it displays only the last entry in read function.

   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);
 }
}

Output:

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

Displaying content of Extension Structure array:

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

It looks like the pointers in the extensions structure are left pointing into buf , which is being overwritten on each pass through the loop (and deallocated on exiting read_conf , so the fact that you're seeing anything valid at all is undefined behavior).

You'll need to call strdup on the strings returned from strtok to get them into newly allocated dynamic memory in order to store them safely. Something like this:

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

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

Just remember to free() the strings returned by strdup when you no longer need them. If by some stroke of ill luck your C environment doesn't provide a strdup function (it's not in ANSI C but is in POSIX), you can supply it -- it's rather simple:

/* 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM