Thread: strtok() and struct with many members

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    58

    strtok() and struct with many members

    I need to separate a long string into smaller peaces with a strtok() and those peaces need to be values of members of a struct. What should I do to achieve that? Inside the while should I put ifs without the else or should I use another method? I will give you the struct so you can help yourselves.

    Code:
    typedef struct
    {
        int year;
        int month;
        int day;
    }date;
    typedef struct
    {
        double latitude;
        double longitude;
    }location;
    typedef struct incList
    {
        /*Gia string ""*/
        char area[100];
        date reported;
        int total_missing;
        int dead_women;
        int dead_men;
        int dead_kids;
        char cause_of_death[150];
        char location_description[500];
        location coordinates;
        char URL[100];
        struct incList *next;
    }incident;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    Can you post an example data line (or two)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    I have to give to the structs data with this. The data I want to pass to the circular list above is the string after the space of the following line. The data I want to give to each member is separated from each other with a semicolon(; ) 1-1.

    newIncident Mediterranean;12/03/2020;5;2;0;1;Drowning,Asphyxiation;Off the coast of Larache, Morocco;35.189860435378, -6.173145606543; https://archive.ph/gB4Vs

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    Like this
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, in ) ) {
        incident event;
        char *token = strtok(buff,";");
        strcpy(event.area, token );
        token = strtok(NULL,";");
        sscanf(token,"%d/%d/%d",&event.reported.day, &event.reported.month, &event.reported.year );
        token = strtok(NULL,";");
        event.total_missing = strtol(token,NULL,0);
        token = strtok(NULL,";");
        // use whatever of strcpy, sscanf, strtol, strtod seems best for the data type
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    So to better understand what you have written:
    1) buff is the large string I want to separate.
    2)BUFSIZ is the size of the array where I put the string before the separation.
    3) can I make the event a pointer and change the dotes with ->
    4)this is the part with only a few of the members and as a result I must fill in the rest with the other members. Am I right?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    Yes

    Yes - specifically, BUFSIZ is declared in stdio.h.

    Yes

    Yes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    So,about 2), I don't have to #define it?

  8. #8
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    Also, about adding new incidents in the list(circular linked list) I must do the same as above and put it inside a for?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    Like this
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, in ) ) {
        incident event;  // This is just a temporary copy
        char *token = strtok(buff,";");
        strcpy(event.area, token );
        // etc etc etc
        insertIntoList(&myList,&event);
    }
    In insertIntoList, you malloc a new instance of incident, copy across all the data from event, and then do your pointer magic to place it into your circular list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2020
    Posts
    58
    So what is myList? insertIntoList is a new struct? The insertIntoList(&myList,&event); is put into your previous part of code?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,110
    > So what is myList?
    A variable - something presumably pointing to your circular list.


    > insertIntoList is a new struct?
    Does it look like a struct to you, what with the ( ) and all.
    It's a FUNCTION call.

    Check all your old posts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok() and struct
    By TheGreekMan2000 in forum C Programming
    Replies: 1
    Last Post: 06-01-2020, 05:17 AM
  2. manipulating struct members
    By nhiap6 in forum C++ Programming
    Replies: 2
    Last Post: 01-23-2014, 03:40 PM
  3. Iterating through Struct Members
    By Vespasian in forum C++ Programming
    Replies: 2
    Last Post: 04-04-2013, 03:20 PM
  4. How to access members of a struct
    By Bnchs in forum C Programming
    Replies: 9
    Last Post: 03-25-2008, 02:28 PM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 12:27 PM

Tags for this Thread