tags:

views:

121

answers:

2

I've got some analysis code (myprog) that sucks in data using the following:

 if(5 == fscanf(in, "%s%lf%f%f%f", tag, & sec, & tgt, & s1, & s2))

which works just fine. But in the situation where I've got data files that are separated by commas, I'm currently doing something like:

 sed 's/,/ /g' data | myprog

Can I modify the format string in the fscanf() function to accept both delimitation formats?

+1  A: 
fscanf(in, "%[^, ]%*[, ]%lf%*[, ]%f%*[, ]%f%*[, ]%f", tag, &sec, &tgt, &s1, &s2)

Should work?

harald
Yeah, I misread it at first... check the new version :)
harald
Fix the first regex (`%[^,]` should be `%[^, ]`) and I'll vote you up.I wasn't familiar with the '*' specifier before now, thanks.
Jamie
You're right, should be fixed now.
harald
A: 

What about this:

char tmp; fscanf(in, "%s%c%lf%c%f%c%f%c%f", tag, &tmp, & sec, &tmp,& tgt, &tmp,& s1, &tmp, & s2)

If you are not going to care what single character delimits your values, just read it and store it in a throw-away variable.

LoudNPossiblyRight
What if there isn't any spaces?
Jamie
I am only assuming that there is a character delimiter from the original question - experimentation is the mother of discovery.
LoudNPossiblyRight