0

Right now I have an array that gets read in from a text file and prints out like so:

1,Spam Sandwich,5
5,Video Games,3
6,Psychology,3
10,Koi Fish,99
15,Tornados,5

with a new line after each line, so the 18th member of the array is a \n in this case.

I'm interested in isolating the number after the last comma in each case but doing it in a way that the integer could have any digits, so it could read 1,Spam Sandwich,999 and isolate the 999 as well.

So far I've been able to isolate the 5 in the first line with this code:

const char delim[2] = "\n";
char *token;

 token = strtok(a.array, delim);

 printf("%s\n", token);

 char *token2;
 const char delim2[2] = ",";

 token2 = token;

 printf("%s\n", token2);

 token2=strtok(token2, delim2);

 printf("%s\n", token2);

 token2 = strtok(NULL, delim2);
 token2 = strtok(NULL, delim2);

 printf("%s\n", token2);

which cuts the first line out of my array, delimited by the \n character, then cuts a different token, token 2, down to what's after the second comma. This seems like an inefficient way to do this if i have an indeterminate amount of lines (i want this to work for any number n lines, not just 5 like my test case)

I feel like I am on the right track with strtok(), but i'm having trouble seeing where I should proceed to.

Suggestions?

user229044
  • 232,980
  • 40
  • 330
  • 338
Bizzle
  • 87
  • 2
  • 8
  • 2
    What you want is called a [CSV parser](https://www.google.com/#q=C+csv+parser) and there are lots of them online you can grab. – TypeIA Feb 11 '14 at 19:50
  • Sorry I should add that this can't be done with any nonstandard libraries. – Bizzle Feb 11 '14 at 19:50
  • 1
    You can still find *megatons* of sample CSV-parsing code using a Google search, much of which is public-domain code which you could copy-and-paste even if you didn't understand it (though hopefully you'd try to learn from it). Sorry for not doing the leg-work for you, but CSV parsing is such a ridiculously common programming task and is so thoroughly covered elsewhere that you can't *not* find it if you spend a little time searching/reading. – TypeIA Feb 11 '14 at 19:54
  • I've actually looked quite a bit and never found an example that seemed right but I guess I'll go back to looking. – Bizzle Feb 11 '14 at 19:59
  • use `strtok` and `strrchr`. or Customize function. – BLUEPIXY Feb 12 '14 at 08:05

2 Answers2

1

You should use fgets to read line-by-line from a file. You can then extract the last number with:

long last_numer = atol(1 + strrchr(str, ','));

strrchr(str, ',') finds the last occurrence of a coma in str, and we add 1 to step over it.

edit. For example:

strrchr("3,My Hawaii Vacation,999", ',') returns a pointer to the 19th character, like so:

3,My Hawaii Vacation,999
                    ^

If you print the return value, it would output ,999

Also I'd like to add (as pointed out in the comments) there are tons of CSV parsers out there and you should use one if you plan on doing more complex processing. However, this task can be solved in straightforward manner with standard functions .

MBlanc
  • 1,773
  • 15
  • 31
  • so 1 step over strchr will give the item directly after the last comma, but what if the number is 999? – Bizzle Feb 11 '14 at 20:01
  • For simple tasks, you should try to keep it simple and avoid using big complicated structures. Please +1 if my answer was useful! :) – MBlanc Feb 11 '14 at 20:03
0

Intuitive solution is loop inside loop strtokenizing your array. Where outer loop use \n as delimetr, and inner loop will use , as delimetr. But this won't work

On the first call to strtok() the string to be parsed should be specified in str. In each subsequent call that should parse the same string

Check strtok_r if it is available, and this answer there is also solution without strtok_r

int main(void)
{
    char str[] = "0,Hello,0\n1,Cooking,0\n2,Biking,1\n";
    char *end;
    char *token = strtok_r(str, "\n", &end);

    while (token != NULL)
    {
        char *end_token; 
        char *token2 = strtok_r(token, ",", &end_token);
        while (token2 != NULL)
        {
            printf("b = %s\n", token2);
            token2 = strtok_r(NULL, ",", &end_token);
        }
        token = strtok_r(NULL, "\n", &end);
    }

    return 0;
}
Community
  • 1
  • 1
Dabo
  • 2,371
  • 2
  • 18
  • 26