Thread: scanf() formatted text...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    scanf() formatted text...

    Code:
    scanf("%[^(]%*c%[^0-9]%d%[^)]%*c%[^0-9]%d%[^-]%*c%[^0-9]%d", &areacode, &prefix, &suffix );
    -quzah

    I saw this in a few posts ago... I was wondering how it works... I can kind of see but I have NEVER seen scanf() used like this before...
    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Eesh. Yeah that's horribly wrong. The original intent was to scanf into 3 variables, skipping everything else. At a glance, I simply omitted '*' in too many places. I also was under the impression that if it didn't encounter what it was looking for when using '*', it would just skip on to the next anyway. Looks like you do in fact learn something new every day.

    I also drasticly over-complicated the issue. The theory was:

    skip everything up to (
    skip (
    skip everything up to 0-9
    read 0-9 into a variable
    skip everything up to )
    skip )
    skip everything up to 0-9
    read 0-9 into a variable
    skip everything up to -
    skip everything up to 0-9
    read 0-9 into a variable

    But as Salem demonstrated, you can simply do it in three reads. I over complicated the issue. (That and I commonly type while talking on the phone, which is not a GoodThing(TM) when trying to write code.)

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 05:13 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. formatted text from file.?
    By Gugge in forum C Programming
    Replies: 2
    Last Post: 03-08-2002, 05:25 PM