Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best way to parse a text file

Status
Not open for further replies.

tmoody

Technical User
Oct 8, 2001
37
US
I am having a little difficulty on what I term a simple exercise. I have a text file that has multiple strings seperated by white space and then a newline character. Using fscanf dosn't work very well, and I was wondering the best way this??

Thanks in advance
Tim Moody
 
I suggest using strtok().

char *tok;
tok = strtok(a, " \t");
while(tok!=NULL)
{
printf("%s\n",tok);
tok = strtok(NULL, " \t");
}

Note that strtok() changes the input array by adding a null in place of the token found.
Hope this helps.
CaKiwi
 
Thanks for the tip. I get a line from the file with fgets(this terminates on newline) then I use strtok to parse the line.
Thanks
Tim Moody
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top