Tokenizing string and assigning to element of struct

+1 Henil Patel · November 23, 2015

This is the Struct I am trying to work with.


I have to open a text file and then sort the list in the text file in the format of the given struct (teamStruct) above. I cannot seem to do so. I have run this test file. Using the while loop and strtok I was able to tokenize the strings and print them exactly as the text file but I am having difficult with the next part where I use the struct teamStructure tPlace part. Any help would be appreciated. Let me know if my question did not make sense. 

Post a Reply


Oldest  Newest  Rating
+1 Dan P · November 23, 2015
Hi Henil,

The naming of your string array is not very clear. You have called it linesize, but it would probably be better called line.

This aside, your code is,

while (read a line from file) {
     printing out the line
     returning the first token from the line to *item

create a struct called tPlace
Then set *item to the address of the teamName array in the struct.

Now print the string from the uninitialized array.

I would suggest that you fix the input first. You need to find a way to have multiple pointers for the tokens.


char *tokens[100];               // This is an array of pointers.
int i, numOfTokens;

i = numOfTokens = 0;
while(read input && i < 100) {
       token[i++] = strtok(line, " ");

for(i = 0; i < numOfTokens; i++) {
      printf("%s\n", token)

Hopefully this will give you an idea of what you need to do to achieve what you want.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator