| Using VISA.NET > Formatted I/O > Using Scanf > Scanf Code Snippets > Reading Strings |
A string argument has a format specifier in one of the following forms:
s[flags][width]s
t[flags][width]t
T[flags][width]T
[][flags][width][<chars>]
[^][flags][width][^<chars>]
|
Modifier |
Interpretation |
|---|---|
|
Default Functionality |
The data in the read buffer is parsed as a string or array of strings. |
|
flags *, Q, q |
Controls parsing, as follows:
|
|
width |
The maximum number of characters to be parsed for this specifier. A hash character (#) may be present in lieu of an integer width modifier, in which case the value is specified in the Scanf inputs array argument. |
Remember that Scanf is reading bytes from the formatted read buffer, parsing them according to the format specifiers, and placing the results in an output variable. The comment preceding each line shows the data being parsed by the snippet, and the resulting value that is returned in the corresponding output argument, like this: // "<data to parse>", "<value>". The <value> may optionally be followed by a comment in parentheses.
Assume that the io variable is a valid reference to IMessageBasedFormattedIO. Assume that resultn is an out argument declared as a .NET String.
Note that for most instruments, an END is added to a newline, so that the behavior of %t and %T are identical.
| Literal Text and Escape Sequences |
Copy Code |
|---|---|
|
// " Hello World\n", "World" (reads literal "Hello" before string) io.Scanf("Hello%s\n", out result1); | |
| No Modifiers |
Copy Code |
|---|---|
|
// " Hello World\n", "Hello" (whitespace delimits %s strings) io.Scanf("%s%*T", out result1); // " Hello World\n", "Hello", "World" (whitespace delimits %s strings) io.Scanf("%ss*T", out result1, out result2); // " Hello World\n", " Hello World\n" io.Scanf("%t", out result1); // " Hello World\n", " Hello World\n" io.Scanf("%T", out result1); // " Hello World\n", " Hello Wo" io.Scanf("%[ HeloW]%*T", out result1); // " Hello World\n", " Hello Wo" io.Scanf("%[^r]%*T", out result1); | |
| Flags |
Copy Code |
|---|---|
|
// " Hello World\n", "World" (whitespace is discarded, buffer is flushed) io.Scanf("%*s%s\n", out result1); // " Hello World\n", "World\n" io.Scanf("%*s%t", out result1); // " Hello World\n", "Hello" io.Scanf("s*T", out result1); // " Hello World\n", "rld\n" io.Scanf("%*[ HeloW]%T ", out result1); // " Hello World\n", "rld\n" io.Scanf("%*[^r]%t", out result1); | |
| Width and Precision Modifiers |
Copy Code |
|---|---|
|
// " Hello World\n", "Hel", "Wor" io.Scanf("%3s3s*t", out result1, out result2); // " Hello World\n", "Hel", "Wor", "" io.Scanf("%#s#st", new Int32[] { 3, 3 }, out result1, out result2, out result3); | |