
-----------------------------------
Fonzie
Mon Oct 22, 2007 9:34 pm

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?

-----------------------------------
Mazer
Mon Oct 22, 2007 10:10 pm

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
Tue Oct 23, 2007 8:07 am

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
Tue Oct 23, 2007 8:42 am

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
Tue Oct 23, 2007 11:42 am

Re: reading string input
-----------------------------------
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
Tue Oct 23, 2007 12:12 pm

Re: reading string input
-----------------------------------
thanks for the help, I just ended up using strintok.

-----------------------------------
md
Tue Oct 23, 2007 6:35 pm

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.
