1

I am currently trying to scan a single line in from a file but having a snag at strings. This is the example line my professor told me to work on.

enum status{MEM,PREP,TRAV}
union type { double int day, char* title, float cost}

13953 P 12 26 2011 1 5 2012 2 A 3.30 249.00 A 2.0 148.00 MEM Cuba Christmas 3 0 2 Sierra Del Rosario, Cuba

I'm fine with everything accept at the point (MEM Cuba Christmas) when I'm scanning it in from a FILE. I read the first part of the data just using fscanf(), but MEM is a enumerated type with a union type that dictates the following input. My problem is with the syntax of the scanning. I tried using getline starting at MEM but I hit snags with the tokenizing since the city / country can have spaces. Not sure what other scans to use I was looking at sscanf() but wasn't sure if it works with files.

UPDATED:

int main(void);
{
 int m, length = 100;
 char *word, file_name[100];
 FILE *file_point
 printf("Please enter file name with .txt extension:");
 scanf("%s", file_name);
 file_point = fopen(file_name,"r");

  while (fscanf(file_point, "%d", &m) != EOF)
  {

  temp.dest_code = m;
  fscanf(file_point, " %c %d %d %d %d %d %d %d",
     &temp.area_code,
     &temp.Smonth, &temp.Sday, &temp.Syear,
     &temp.Emonth, &temp.Eday, &temp.Eyear,
     &temp.leg_num);
  for (n=0; n < temp.leg_num; n++)
    {
      fscanf(file_point," %c %f %f",
         &temp.tleg[n].travel_type,
         &temp.tleg[n].travel_time,
         &temp.tleg[n].cost);
    }
  fscanf(file_point," %d %d %d ",
     &temp.adult,
     &temp.child,
     &temp.infant);


  temp_name = (char *)malloc(length + 1);
  getline (&temp_name, &length, file_point);

  word = strtok(temp_name, ",");

  temp.dest_name=(char *)malloc(strlen(word)+1);
  strcpy(temp.dest_name, word);

  word = strtok(NULL, ",");

  temp.dest_country=(char *)malloc(strlen(word)+1);
  strcpy(temp.dest_country,word2);

  printf("name:%s country:%s\n", temp.dest_name, temp.dest_country);
      printf("adult:%d , child:%d , infant:%d \n", temp.adult, temp.child, temp.infant);

   }
}

This was the code I was using as a base that I came up with but not sure how to handle the enumerated and union. I was thinking of doing something like:

getline(&status, &length, file_point);

but how do I convert string to integer or float?

3
  • Oh yea forgot to mention that I'm scanning this single line in from a file May 9, 2011 at 0:24
  • could you post some code, to show us what have you tried to do since now?
    – Heisenbug
    May 9, 2011 at 0:41
  • The enum needs a semi-colon at the end. The union also needs a semi-colon at the end, and the commas need to be semi-colons. However, if you've got your code compiling, then the trouble is that you did not copy and paste your code. May 9, 2011 at 5:35

2 Answers 2

2

If I understand your problem properly (I'm not sure I do), then you face the problem of seeing 'MEM' (or 'PREP' or 'TRAV') as a string in the input, and you have to understand how to handle the following data. The enum suggests that you might want to convert the string MEM to the value of MEM in the enumeration.

It is hard to fully automate such a conversion. It would be simplest simply to recognize the strings and decide what to do based on the string:

if (strcmp(found_string, "MEM") == 0)
    ...do the MEM stuff...
else if (strcmp(found_string, "PREP") == 0)
    ...do the PREP stuff...
else if (strcmp(found_string, "TRAV") == 0)
    ...do the TRAV stuff...
else
    ...report unknown type code...

However, you can create a structure to handle the conversion from string to enumeration value.

struct StateConv
{
     const char *string;
     enum state  number;
};

static struct StateConv converter[] =
{
    { "MEM",  MEM  },
    { "PREP", PREP },
    { "TRAV", TRAV },
};
enum { NUM_STATECONV = sizeof(converter) / sizeof(converter[0]) };

enum state state_conversion(const char *string)
{
    for (int i = 0; i < NUM_STATECONV; i++)
    {
        if (strcmp(string, converter[i].string) == 0)
            return(converter[i].number);
    }
    fprintf(stderr, "Failed to find conversion for %s\n", string);
    exit(1);
}

You need a better error handling strategy than 'exit on error'.

Your scanning code will need to read the word, and then call state_conversion(). Then depending on what you get back, you can read the remaining (following) data in the correct way for the state you were given.

1
  • ah okay I was getting a lot of errors trying to treat it as a string May 9, 2011 at 13:53
1

No, you can't do that in the way you are trying. MEM in your file is a string type, you need to parse it like you parse a string and then set the value of your enum according to that string. For example, when you want to parse your status type (MEM,PREP,TRAV):

char typeBuffer[6];
fscanf(file_point,"%5s",typeBuffer);

Then manually compare the content of typeBuffer:

status stat;
if (strcmp(typeBuffer, "MEM") == 0){
     stat = MEM;
}

The conversion between string type and enum cannot be implicit.

1
  • The typeBuffer is one too small for reading 'PREP' or 'TRAV' - you need at least 5 there, and probably more to allow for mistakes. It would not be a bad idea to use "%5s" and dimension 6; that allows you to reject 'TRAVEL' without overflowing the variable. May 9, 2011 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.