John
John

Reputation: 804

what is wrong with the following strtok() usage?

I am using vc2010 and I am trying to read contents of a file into a struct as follows and it gives me run time error.

char buf[100];
char *token = NULL; 
while( fgets (buf , 100 , rd) != NULL )
{
    token = strtok( buf,", ");
    test_st.fp.chunk_offset = atol(token); 
    printf("\n %llu ", test_st.fp.chunk_offset); 

    //OPTION 1: if i do this there will be no runtime error but the same
    // value as the first token will be assigned to chunk_length
    token = strtok(buf, ",");
    //OPTION 2: this line gives error in the second while loop iteration
    token=strtok(NULL,",");
    test_st.fp.chunk_length = atol(token);
    printf("%llu ",  test_st.fp.chunk_length);  
    token = strtok(NULL, " ");
    ....
}

my other problem is how can i use strtok() or any- if there is one- to assign a very long character string(like the one after the second coma in the following file content) into a structure data member(in this case into fp.fing_print)?

Here is the first part of the file i am trying to read

0,4096,2ed692b40e335f7c20b71c5ed0486634
4096,3028,da40bf20c8ff189087b8bd7e8580118a
7124,2177,e6dfaee81e96095d302c82f9fd73dc55
9301,1128,76201eadff3c89a381a314ed311f75ff

the structure definition i am trying to read this into is:

typedef struct fpinfo
{
  unsigned long chunk_offset;
  unsigned long chunk_length;
  unsigned char fing_print[32];
}fpinfo;

typedef struct Hash_Entry {
   struct Hash_Entry *next;  /* Link entries within same bucket. */
   unsigned namehash;        /* hash value of key */
   struct fpinfo fp;
} Hash_Entry;

EDIT:
Yes you are right I have to use the second option.
I HAVE FOUND THE PROBLEM WITH THIS ONE. The file have empty lines between every line and it gives error message when it gets to these empty lines.

BUT I AM STUCK in the other problem where I have to read the last part of each line into the array member(fp.fing_print) of the structure.

Thank you everybody for your help!!!

EDIT: the fing_print[32] array is supposed to hold 32 characters which are results of an md5 hash function. I am not sure if i have to make it a null terminated string or not. if you guys could give me a tip about it I will be more than grateful.

Upvotes: 0

Views: 1363

Answers (2)

hmjd
hmjd

Reputation: 122011

As stated by paulsm4 you need to check strtok() return value. The following code works and includes the creation of the string:

while( fgets (buf , 100 , rd) != NULL )
{
    token = strtok(buf,", \n");
    if (0 != token)
    {
        test_st.fp.chunk_offset = atol(token); 
        printf("\tchunk_offset=%lu\n", test_st.fp.chunk_offset); 

        token = strtok(0, ", \n");
    }

    if (0 != token)
    {
        test_st.fp.chunk_length = atol(token);
        printf("\tchunk_length=%lu\n", test_st.fp.chunk_length);  
        token = strtok(0, ", \n");
    }

    if (0 != token)
    {
        /* EDIT: fing_print datatype changed. */
        memset(test_st.fp.fing_print, 0, sizeof(test_st.fp.fing_print));
        strncpy(test_st.fp.fing_print,
                token,
                sizeof(test_st.fp.fing_print) - 1);

        /* test_st.fp.fing_print = _strdup(token); */

        printf("\tfing_print=[%s]\n", test_st.fp.fing_print);
    }

    printf("\n");
}

Delimiter set is ", \n" as fgets() will read the newline character into buf.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121869

You need to check for NULL return:

  if ((token = strtok(buf, ",")) {
    // must have found a comma
  }
  if (token && (token = strtok(NULL,"'")) {
    // Two in a row: I found a comma and then I found an apostrophe
  }
  ...

Upvotes: 1

Related Questions