Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > scanf help

Reply
Thread Tools

scanf help

 
 
Stu
Guest
Posts: n/a
 
      10-11-2006
Can somebody give me a scanf statment that will parse out the value
between the pipe signs but not including the the pipe signs.


@xxxthis value|

Thanks to all that answer this post.

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      10-11-2006
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.
>
>
> @xxxthis 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
 
Reply With Quote
 
 
 
 
Bill Reid
Guest
Posts: n/a
 
      10-12-2006

Stu <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.
>
>
> @xxxthis 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



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-13-2006
Bill Reid wrote:
> Stu <(E-Mail Removed)> 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.
>>
>> @xxxthis 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/>


 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      10-13-2006
In article <(E-Mail Removed)>,
CBFalconer <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
Help with scanf Sivarn C Programming 23 04-13-2004 03:09 PM
scanf help again Ramprasad A Padmanabhan C Programming 3 08-08-2003 05:30 AM
help with infinite loops and scanf Rob C Programming 8 07-29-2003 12:28 AM



Advertisments