Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   scanf help (http://www.velocityreviews.com/forums/t444714-scanf-help.html)

Stu 10-11-2006 06:12 PM

scanf help
 
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.


jacob navia 10-11-2006 06:24 PM

Re: scanf help
 
Stu wrote:
> 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

Bill Reid 10-12-2006 11:16 PM

Re: scanf help
 

Stu <beefstu350@hotmail.com> wrote in message
news:1160590368.732446.324370@m7g2000cwm.googlegro ups.com...
> 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...

---
William Ernest Reid




CBFalconer 10-13-2006 12:10 AM

Re: scanf help
 
Bill Reid wrote:
> Stu <beefstu350@hotmail.com> wrote in message
>
>> 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:
<news:news.announce.newusers
<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/>



Richard Tobin 10-13-2006 11:55 AM

Re: scanf help
 
In article <452ED97D.27E487F4@yahoo.com>,
CBFalconer <cbfalconer@maineline.net> wrote:

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


>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


All times are GMT. The time now is 09:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.