From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
02-09-2015 09:12 AM
I want to scan a string which is made from a fixed number of strings comma separated.
For example
source string = str1,str2,str3,str4
I want to extract the four strings "str1", "str2", "str3" and "str4".
The problem is that one (or more of the strings) can be empty.
For example
source string = str1,str2,,str4
I want to extract the four strings "str1, "str2", "" (i.e. empty string), "str4"
All the functions I tested (sscanf, Scan , strtok) handle multiple occurrences of delimiter as single occurrence and so I get
"str1", "str2", "str3
Which function can I use?
02-10-2015 01:46 AM
Hello vix!
Unfortunately, the strtok function will not be able to detect empty tokens.
However, if your requirement is to detect empty tokens, it's not very hard to implement such a function of your own: a simple implementation of such a function would be using a for-loop which scans the input string, and extracts substrings beginning at the last position of the "," and ending at the position of the next ",". You probably also need to perform various limit use cases (e.g. empty input string, input string ending with delimiter, etc.).
Best regards!
- Johannes
02-10-2015 01:52 AM
Hello Johannes,
I know I can quite easily implement my own function to parse the string, but this is true for many of the existing functions inside a CVI library
I also know strtok isn't able to find empty tokens, but what about Scan()?
As far as I understand from the help, it should be able to do this. Or not?
02-10-2015 02:36 AM
The Scan function isn't able to detect an empty character string, as it will detect your second "," as the beginning of your next token.
02-10-2015 07:48 AM
I use Scan() with the option %s[xt44]
and from CVI Help
tn | Terminate on character | ... When applied to a target string that the functions fill from a source string, the t modifier specifies that the functions stop filling the target on the first occurrence of the character n, where n is the ASCII value of the character. Thus, %s[t59] causes the functions to stop reading the source string on an ASCII semicolon. More than one t modifier can occur in the same specifier, in which case the functions stop filling the target when any of the terminators occur. If no t modifier exists, the functions stop filling the target on any whitespace character. |
As far as I understand, this description means that Scan() should be able to detect an empty token.
Am I wrong? Why?