Thread: Using strtok to fill a structure from a text file?

  1. #1
    Registered User
    Join Date
    Oct 2010

    Using strtok to fill a structure from a text file?

    Hi, I have a text file with a list that reads like this: Last name, First name, Grade with the names being strings and the grades being integers. I need to put this list into a structure so that I can manipulate the list using any of the three sections. I know that using strtok and setting the delimiter as the comma will cut the list into the pieces I need, but I don't know how to put each piece into a separate area of the structure. This is what I have so far:

    #include <stdio.h>
    #include <string.h>
    //Create the structure
    typedef struct{
    	char last_name[30];
    	char first_name[20];
    	float grade;
    } class_data;
    main() {
    	FILE fp*;
    	fp = fopen(file, "r");
    //Find the length of the list
    	int i = 0;
    	while(fgets(string, 75, fp)!=NULL) {
    	class_data student[i];
    	int counter;
    	char token[30];
    	for(counter=0; counter<i, counter++) {
    		while(fgets(string, 75, fp)!=NULL) {
    			token = strtok(string, ",");
    Can anybody give me some guidance on how to put the information into the structure? As soon as I get that worked out, the rest will be easy but I can't find anything like this online. Thank you!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    So there are a few problems. First, you should always read up on the library functions you're using, in this case strtok: strtok(3): extract tokens from strings - Linux man page. You only pass string in the first time.

    Second, there's a big problem with your file reading. and loops. Your first while(fgets) loop reads the file all the way to the end. That leaves the the file position at the end and the EOF flag set for fp. You would need to call fseek or rewind before your second while(fgets) loop.

    Also, you don't want that second while loop nested inside the for loop. That will process the entire for each student, not only making this dog slow, but giving incorrect results. Drop the for loop, initialize i to 0 above the second while loop, and increment it at the end of the while loop.

    You address each student record in your array like so:
    Then you would use strcpy to get the last and first names into your structure, and use a simple assignment ('=' operator) with an atof call for the grade.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM