Posted: Mon Oct 22, 2007 9:34 pm Post subject: reading string input
let's say I have a string like "time 12 date 4 year 3" where "time" "date" and "year" are variable, meaning other words might be in their place. The numbers are also variable. I want to be able to take input from a datafile of this format and analyze each word. So I want to see what this first word is, is it "time" or is it "food"? etc etc. Same for the other fields. The only way I can think of doing this is using getchar until I reach white space and then comparing if the words contained within the white space match the words I'm looking for. Is there any better way to do this? Is there a function the just reads in strings up to white space?
Sponsor Sponsor
Mazer
Posted: Mon Oct 22, 2007 10:10 pm Post subject: RE:reading string input
I haven't used C in a while, but can't you use fgets to get up to X characters, or until you reach a given token?
Fonzie
Posted: Tue Oct 23, 2007 8:07 am Post subject: Re: reading string input
as far as I can tell, you can't read up to a given token (which is what I would really like to do) with that function unless I'm reading something wrong.
Mazer
Posted: Tue Oct 23, 2007 8:42 am Post subject: RE:reading string input
You're right, sorry. I could've sworn you can supply your own token but it looks like it only uses '\n'.
OneOffDriveByPoster
Posted: Tue Oct 23, 2007 11:42 am Post subject: Re: reading string input
Fonzie @ Mon Oct 22, 2007 9:34 pm wrote:
Is there a function the just reads in strings up to white space?
There might be. Look into scanf(). Of course you ought to know how long the "words" are.
Fonzie
Posted: Tue Oct 23, 2007 12:12 pm Post subject: Re: reading string input
thanks for the help, I just ended up using strintok.
md
Posted: Tue Oct 23, 2007 6:35 pm Post subject: RE:reading string input
There is always reading a character at a time (or many characters into a buffer, and then one character at a time) and doing your own token parsing.