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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shan Yang
    New Member
    • Jan 2011
    • 1

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

    Hi,

    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


    Code:
    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?

    Thanks!

    Shan
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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.
    Code:
        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.

    Comment

    Working...