Thread: Need help with swscanf function

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    42

    Need help with swscanf function

    So I started a new program and I'm using wide characters (wchar_t). This is all new to me and I'm having a hard time adjusting to the new syntax. One of the problems I'm having is trying to read a line from a file. I normally use sscanf. But I now have to use swscanf. Here's the line:

    card is a struct pointer passed into the function
    This is what readLine is:
    Code:
    1		1	2	FALLEN TITAN*	1		4		304		1880	0	d
    Code:
    swscanf(readLine,L"%d %d %d %[^*] %d %d %d %d %d %[^\n]",&card->copies,
                                                             &card->level,
                                                             &card->type,
                                                             card->name,
                                                             &card->rarity,
                                                             &card->faction,
                                                             &card->damage,
                                                             &card->hp,
                                                             &card->specialLetters,
                                                             card->traits);
    The issue with this is that the first 3 ints are read fine, and then the string is read fine. But everything after the string is zero and the last string (traits) is empty. I'm pretty sure it has something to do with my %[^*]. I used that in sscanf to read all characters up to the asterisk. That allowed me to read strings with spaces because the asterisk acts like a delimiter.

    Any ideas on what is going wrong?
    Last edited by edomingox; 1 Week Ago at 04:52 PM.

  2. #2
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    618
    Two problems. If name and traits are wchar_t strings then you need to put an l (lowercase L) before the opening square brackets in %[^*] and %[^\n]. They don't automatically read wchar_t strings.

    However, it wouldn't work even with regular strings since your first string is ended by an asterisk (according to your format) but you never eat the asterisk. So it should be more like

    Code:
    L"%d %d %d %l[^*]* %d %d %d %d %d %l[^\n]"
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    42
    Thanks for the advice john. I tried putting l before the bracket and it didn't work.
    I added what readLine is in my first post (probably while you were responding), I have the asterisk there. This does work in my other programs when I'm using sscanf. I don't use wide characters in my other programs. I thought I would try it here but it's beginning to be more trouble than what it's worth.

    Edit: I take that back. It worked. When I read your line, I didn't notice the asterisk after the bracket. That worked out as expected. Thanks.
    Last edited by edomingox; 1 Week Ago at 05:04 PM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    42
    Quote Originally Posted by john.c View Post
    Two problems. If name and traits are wchar_t strings then you need to put an l (lowercase L) before the opening square brackets in %[^*] and %[^\n]. They don't automatically read wchar_t strings.

    However, it wouldn't work even with regular strings since your first string is ended by an asterisk (according to your format) but you never eat the asterisk. So it should be more like

    Code:
    L"%d %d %d %l[^*]* %d %d %d %d %d %l[^\n]"
    I'm a dummy. You're right from the start. In my other programs, I used %[^*]*. I didn't even see that extra asterisk I put there.

  5. #5
    Registered User john.c's Avatar
    Join Date
    Dec 2017
    Posts
    618
    I'm a dummy.
    Nah. Format strings are notoriously fiddly things.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-14-2017, 07:25 PM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM

Tags for this Thread