-1

I have to to read a textfile with student first names, and last names. As per the twisted program requirements, the first and last names are char POINTERS. Each line is read using fgets. Hence, I have the variables char *last_name, char *first_name in an array struct.

For example:

Bill, Clinton

William, Gates

I am required to allocate their size during runtime by using strlen with the strtok pointer...

The issue is that if I use strtok with strlen to find out the name's length, the original data_line is already broken up! Does anyone know how to do this?

  • I'm not sure exactly what you're asking, but `strtok` keeps track of where it left off, so it can still be used to get the next token. – Michael Burr Dec 02 '12 at 01:54
  • I will provide an example. Supposing I strtok data_line and get "Bill". I use strlen to find out that the size is for. I then call, "(**person)[i].first_name = (char*)malloc((strlen(strtok(data_line, ",")) + 1) * sizeof(char);" This is an issue, because I can't assign the strtok data to first_name since I already used it once. – Dinesh Jay Dec 02 '12 at 02:06
  • @DineshJay it could help if you show your code – Anon Dec 02 '12 at 02:55
  • I am copying it as we speak. – Dinesh Jay Dec 02 '12 at 03:07

1 Answers1

1

You've got all of the primary functions you'll need already named, so you're well on your way.

It makes sense that the names are char pointers. There is no string type in C, just arrays of characters. Also--and this is confusing--arrays and pointers are related in C. They're not the same, but they act the same in a number of cases. Consider that a function requiring a char pointer argument can accept either a char pointer or an array name (without the index). Also, notice that an array with an index is really just de-referencing a pointer to a certain address with the index indicating the offset.

So if you want a dynamically sized string, you're going to have to declare it as a char pointer and allocate memory for it dynamically (i.e., malloc). In your case, each first and last name varies in size--and you have one pointer for each--so you'll want one malloc for each of those. Using strlen() is still appropriate, as is using strtok() with multiple separator characters.

You may want to look at an example of strtok()--it can (and will) return each token one at a time. This link has a good example between the question and explanatory answers.

Edit: To be more specific, here is the core of what you're asking in code:

Getting a token:

token = strtok(data_line, sep);  // First token on data_line

Or:

token = strtok(NULL, sep); // Subsequent tokens on data_line

Then, example of allocating memory and storing the first name (last name is entirely analogous):

person[i].first_name = malloc(strlen(token) + 1);
strcpy(person[i].first_name, token);

The value of one added to strlen() result is to make space for the null terminator.

Edit 2: Rather than using malloc() and strcpy(), strdup() accomplishes both and would be preferable.

Community
  • 1
  • 1
David Duncan
  • 1,235
  • 8
  • 14
  • No no no. I know how to use strtok(). I used it to parse a html file. I need to know how to dynamically use strlen on strtok to determine how much I should malloc, then assign that strtok value to the array I just dynamically allocated. I do not know how many letters are in each first and last name! – Dinesh Jay Dec 02 '12 at 03:05
  • 1
    Or to make the whole malloc/copy operation cleaner (and with less chance for a bug to creep in): `person[i].firstname = strdup(token);` – Michael Burr Dec 03 '12 at 00:18
  • Thanks much; dunno why I didn't think of that when answering. (I added this suggestion to my answer.) – David Duncan Dec 03 '12 at 00:27