×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

sscanf format

sscanf format

sscanf format

(OP)
I've been reading about sscanf and it's ability to parse strings.  I'm now very confused about the proper usage.

I want to parse a full file name string into independent name and respective extension but the proper syntax eludes me.  I keep getting confused.

I figure that it needs to look something like this but it doesn't appear to work properly:

CODE

sscanf( full_filename, "%[^.]s.%s", filename, extension );

Can someone help me the proper usage of the "[" and"[^" ?

mike

RE: sscanf format

[ is a scan set, meaning you want to match a particular set of characters
^ means NOT (anything which doesn't match the set)

So %[^.]s.%s matches a string of characters which are not ., followed by a literal ., then an ordinary string (delimited by whitespace)

Note if you have first.second.ext it will match the FIRST . and not the last.

 

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

RE: sscanf format

(OP)
Thank you.  I played with it for a while and got some strange result UNTIL I figured out that I had improperly defined the extension string.  I forgot that the string "extension" has to be 4 char long rather than 3.  I had forgotten to make room for the trailing "\0".

In the end, the code looked like this:

CODE

i = sscanf( argv[1], "%[^.].%s", filename, extension );

It'll take a while but someday this stuff will come easier to me.

thanks again,

mike

RE: sscanf format

Be careful, it's not so easy:
- max extension part length exceeeds 3 on Windows, Unix and Linux
- that's correct file name on these systems: one.two.three
- there are file names without extensions
Moral: sscanf is not an adequate tool for file name parsing.
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close