Your code works fine for me. Your initial code should read one line and tokenize it. I did this:
Code:
#include <stdio.h>
#include <string.h>
int main( int argc, char* argv[] )
{
char *str1, *str2, *token, *saveptr1, *saveptr2, *subtoken;
int j;
char buf[50] = ",Hey,Dude,dfa,\n,Hey,Dude,dfa,\n";
for (j = 1, str1 = buf; ; j++, str1 = NULL) {
token = strtok(str1, "\n");
if (token == NULL)
break;
printf("%d: %s\n", j, token);
for (str2 = token; ; str2 = NULL) {
subtoken = strtok(str2, ",");//fprintf(fp2,subtoken);
if (subtoken == NULL)
break;
printf("t --> %s\n", subtoken);
}
}
}
It reads only one line. Thus, it stops on the first \n. I guess this is not what you want eh? You would like it to print everything in buf...
If you don't do the subtokenize thing it works fine. I don't think you can use strtok() the way you do. Maybe with temporary char[] ?