Tokenizing string and assigning to element of struct

+1 Henil Patel · November 23, 2015
/images/forum/upload/2015-11-22/7b09c05bf3eb3037e5be2b53354c9ed3.png

This is the Struct I am trying to work with.



/images/forum/upload/2015-11-22/5e56c8738ca6889d9507cbedd1b929cf.png

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

Replies

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.

ie.

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

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

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

C

107,085 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator