469,929 Members | 1,569 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

How to parse string with sscanf that have a empty field?


I am handling tab delimited txt files that have multiple fields. But some of the fields can be empty. So it will appear as <TAB><TAB> in the file rather than <TAB>something<TAB>. If I read in the line and then use something like

Expand|Select|Wrap|Line Numbers
  1. sscanf(buf0, "%s %ld %ld %s %s %s %s %s", chr, &bg, &ed, zg, var, ref, s1, s2);
The program will skip these empty field and assign the value of the next non-empty field to the variable. What I really want it to do is to assign an empty string (length of 0) to the variable.

How can I write the sscan file to do the scanning? Of course, I can read the line character by character and check if anything is a <TAB> and turned the substring into a field value but that is a little awkard to my opinion. Any good and simple way to do this?


Jan 28 '11 #1
1 5188
1,510 Expert 1GB
if the format of individual lines varies I don't think you have any choice but to read each element individually and work out what it is, i.e. a <TAB> or a string.

You ould read the whole line in as a string then have a function which looks at the next element and returns "" if it is a TAB else the string, e.g.
Expand|Select|Wrap|Line Numbers
  1.     char* nextelement(char *inputstring, char *result)
it would return a pointer to the start of the next elelemnt to read - used when you call the function next time.
Jan 29 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Daniel Lidström | last post: by
4 posts views Thread by Marcel Brekelmans | last post: by
1 post views Thread by Oleg Ogurok | last post: by
3 posts views Thread by Joachim | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.