All pastes #792733 Raw Edit

Mine

public c v1 · immutable
·published 2007-11-21 17:48 UTC
rendered paste body
typedef struct device {  char *name;} dev;intmain (void) {  FILE *fp;  const int line_length=512;  char config_line[line_length];  const char *filename="config.txt";  dev device_list[128];  int attr=0;  int i=0;  int num_devices=0;  char device_name[32];  while (1) {      if (NULL == (fp = fopen(filename, "r"))) {        printf("file open failed for %s\n", filename);        exit (EXIT_FAILURE);      }      while(fgets(config_line, sizeof(config_line), fp) != NULL) {        sscanf(config_line, "%s %d", &device_name, &attr);        device_list[num_devices].name=device_name;        printf("device %d has name %s\n", num_devices, device_list[num_devices].name);        ++num_devices;      }      fclose(fp);      printf("num_devices read from configuration file: %d\n", num_devices);      printf("printing array of structures\n");      for(i=0;i<num_devices;i++) {        printf("device %d has name %s\n", i, device_list[i].name);      }      sleep (7);  }  exit (EXIT_SUCCESS);}Result is this:----------------device 0 has name /dev/hdadevice 1 has name /dev/hdbdevice 2 has name /dev/hdcnum_devices read from configuration file: 3printing array of structuresdevice 0 has name /dev/hdc    <-- why isn't it /dev/hda??device 1 has name /dev/hdc    <-- why isn't it /dev/hdb??device 2 has name /dev/hdcfor an input file:------------------/dev/hda 194/dev/hdb 123/dev/hdc 756