scanf help

S

Stu

Can somebody give me a scanf statment that will parse out the value
between the pipe signs but not including the the pipe signs.


@xxx:|this value|

Thanks to all that answer this post.
 
J

jacob navia

Stu said:
Can somebody give me a scanf statment that will parse out the value
between the pipe signs but not including the the pipe signs.


@xxx:|this value|

Thanks to all that answer this post.
Why scanf?

Just read the line in a buffer, then:


char *start = strchr(line,'|'); // find the first |
char *end = strrchr(line,'|'); // find the last |
if (start)
start++; // start points to the start of the data
else {
// treat error
}
if (end) {
// Zero terminate the data
*end = 0;
}
else {
// reat error
}
strcpy(destination_buffer,start);

This will parse
@xxx;| this value with embedded | signs that are ignored|
correctly, unlike scanf. Besides the code is much simpler.
Scanf is kind of too fragile to be used in serious contexts.

(My personal opinion)

jacob
 
B

Bill Reid

Stu said:
Can somebody give me a scanf statment that will parse out the value
between the pipe signs but not including the the pipe signs.


@xxx:|this value|

Thanks to all that answer this post.
Uh, aside from the usual advice not to use scanf (too dangerous),
and assuming you're trying to get the strings between the "pipes",
use this type of expression:

"%[^|]|%[^|]|"

The "^" inside the brackets indicates the characters NOT to include
in the string...
 
C

CBFalconer

Bill said:
Stu said:
Can somebody give me a scanf statment that will parse out the value
between the pipe signs but not including the the pipe signs.

@xxx:|this value|

Uh, aside from the usual advice not to use scanf (too dangerous),
and assuming you're trying to get the strings between the "pipes",
use this type of expression:

"%[^|]|%[^|]|"

The "^" inside the brackets indicates the characters NOT to include
in the string...

For the efficiency buffs, remember that regular expression use
almost always involves considerable CPU time. A simple routing
such as toksplit is much more efficient.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
R

Richard Tobin

"%[^|]|%[^|]|"
[/QUOTE]
For the efficiency buffs, remember that regular expression use
almost always involves considerable CPU time.

scanf()'s %[^...] notation is not a regular expression, but something
much simpler.

More generally, regular expressions can be compiled into very
time-efficient finite state machines, though certain complicated ones
require impractically large amounts of space.

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

scanf internals 11
about scanf() 2
scanf() 2
Problem with scanf 7
Q for a source code in an exercise 1
question about scanf 11
Help! (Beginner) 2
scanf problem 14

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top