I am trying to split a line using strtok which has tab delimiter.My code and input is as below. I would like to store these tokens into field1,field2,field3.
Code:
while(fgets(line,80,fp1)!=NULL) //Reading each line from file to calculate the file size.
{
field1=strtok(line," ");
//field1=strtok(NULL,"");
field2=strtok(NULL," ");
field3=strtok(NULL," ");
if(flag != 0)
printf("%s",field1);
flag++;
}
Input:
315 316 0.013
315 317 0.022
316 317 0.028
316 318 0.113
316 319 0.133
318 319 0.051
320 324 0.054
321 322 0.054
My current output:(If I print field1)
315 316 0.013
315 317 0.022
316 317 0.028
316 318 0.113
316 319 0.133
318 319 0.051
320 324 0.054
321 322 0.054
I would suggest just using sscanf. It handles tabs as delimiters for you.