Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sscanf format

Status
Not open for further replies.

mjpearson

Technical User
Dec 13, 2002
196
US
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
 
[ 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.
 
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
 
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.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top