Thread: Problem with seperating strings with delimiters..

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    Problem with seperating strings with delimiters..

    Hey there~ Im having some problem with file reading and writing...Im suppose to read an external file which have comma seperated values..So I'm suppose to seperate each part (and store it as Name, Race, Class, Lvl, Guild) and store it in a structure. But there's some problem..Here's my code:
    Code:
    /* Header files. */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    /* Constants */
    #define MAXSIZE 70
    #define NAME_LEN 12
    #define RACE_LEN 20
    #define CLASS_LEN 7
    #define GUILD_LEN 30
    
    typedef struct recordStruct
    {
      char name[NAME_LEN];
      char race[RACE_LEN];
      char class[CLASS_LEN];
      char lvl;
      char guild[GUILD_LEN];
    } Record;
    
    /* Main Program which takes in arguments from Unix*/
    int main(int argc, char *argv[])
    {
      /* Variable Declarations */
      Record *record;
      FILE *in, *out;
      char *rec, *field;
      int field_num;
    
      in = fopen(argv[1], "r");
      out = fopen(argv[2], "wb");
    
      /* Allocates Memory */
      rec = malloc(MAXSIZE * sizeof(char));
      record = malloc(sizeof(Record));
      field = malloc(MAXSIZE * sizeof(char));
    
      while(fgets(rec, MAXSIZE, in) != NULL)
      {
        field = strtok(rec, ",");
    
        field_num = 1;
    
        while(field != NULL)
        {
    
          switch(field_num)
          {
            case 1:
              strcpy(record->name,field);break;
            case 2:
              strcpy(record->race,field);break;
            case 3:
              strcpy(record->class,field);break;
            case 4:
              record->lvl = atoi(field);break;
            case 5:
              strcpy(record->guild,field);break;
          }  
    
          field = strtok(NULL,",");
    
          field_num++;
        }
    
        printf("%s,%s,%s,%d,%s\n",record->name, record->race, record->class, record->lvl,record->guild);
    
        fwrite(record,sizeof(Record),1,out);
      }
    
      /* Free allocated memory */
      free(rec);
      free(field);
    
      /* Close File */
      fclose(in);
      fclose(out);
    
      return EXIT_SUCCESS;
    }
    Here's my external file: (there may be 4 or 5 fields in each line meaning the last can be a null value)
    Code:
    Aoere,Afternoon Elf,Mage,3,
    Mosrdbrann,Mailing List Troll,Priest,29,Fiery Legion
    Irwumory,Evening Elf,Druid,7,The Forgivers
    Schakdynle,Afternoon Elf,Priest,47,Blessed Warriors
    Uathakil,Evening Elf,Paladin,50,Online Fighters
    Iazhath,Newsgroup Troll,Druid,57,
    Btw I'm supposed to store Lvl in one byte only so I'm using char. Anyway i got some funny output..In my printf part it is suppose to display the same output as my external file but this is the output i get:
    Code:
    Aoere,Afternoon Elf,Mage,3,
    Mosrdbrann,Mailing List Troll,Priest,29,Fiery Legion
    Irwumory,Evening Elf,Druid,7,The Forgivers
    Schakdynle,Afternoon Elf,Priest,47,Blessed Warriors
    Uathakil,Evening Elf,Paladin2OnlineFighters,50,Online Fighters  --> this line!
    Iazhath,Newsgroup Troll,Druid,57,Online Fighters                --> and this line!
    It's really long but..I hope someone can help me out with this! Hope i have provided sufficient information! Thanks a lot!!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your final line in your file doesn't have a "guild". As such, you end up printing the last guild over again. You'll have to figure out some way to account for that, as well as any other omissions.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Hey thanks for the quick reply~!! The last line was printed out weirdly because it can have either 4 or 5 values...So i think I'll come up with another way to do that..But i think the 4th line is different some how! I commented out the part where I "record->lvl = atoi(field)" and everything prints out nicely(except for the last line).. The thing prints out nicely too when i commented "strcpy(record->class,field)" and uncomment the atoi part...Totally lost here~ Thanks for your reply again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  2. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  3. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  4. Newbie having problem with strings
    By sunzoner in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2002, 08:34 PM
  5. problem with strings (explained)
    By dune911 in forum C Programming
    Replies: 9
    Last Post: 02-11-2002, 06:02 PM