fwscanf not doing the same thing as fscanf

This is a discussion on fwscanf not doing the same thing as fscanf within the C++ Programming forums, part of the General Programming Boards category; here's the working code Code: char a[16], b[16]; while (fscanf(file, "%s", a) != EOF && fscanf(file, "%s", b) != EOF) ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    188

    fwscanf not doing the same thing as fscanf

    here's the working code
    Code:
    char a[16], b[16];
    while (fscanf(file, "%s", a) != EOF && fscanf(file, "%s", b) != EOF)
    {
      // do stuff
    }
    here's the unicode version, which is pretty much exactly the same.
    Code:
    wchar_t a[16], b[16];
    while (fwscanf(file, L"%s", a) != EOF && fwscanf(file, L"%s", b) != EOF)
    {
      // do stuff
    }
    the text file i'm reading is very simple, something like this:
    AAA A
    BBB B
    CCC C

    the non-unicode will wrap around the parse the next line. the unicode version loops indefinitely the first line.

    what am i doing wrong? thanks.
    Last edited by bling; 08-15-2008 at 11:09 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure why it would not work right in fwscanf(), but in fwscanf(), if you use a wchar_t a[...] as input, you probably want "%ls" or "%S" to indicate that you have a wchar_t string, rather than char string.

    At least that's what it says here:
    http://www.manpagez.com/man/3/wscanf/

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    thanks for the quick reply. yeah i'm not sure why it didn't work either. i'm guessing since fwscanf is a microsoft implementation it might not be 100% the same as they claim...

    changing the comparison to > 0 (instead of != EOF), works.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You do realize, I guess, that you could do all of your f(w)scanf in one line, something like this:
    Code:
    while (fscanf(file, "%s%s", a, b) == 2)
    ...
    Where 2 is the number of "%" format specifiers. If it is not 2, then that particular line is malformed, assuming you expect ALL lines to have two things on them.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    thanks
    i realized it when i was going back to my code after posting and thinking...wait a minute...!!!

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    53
    The *scanf() functions return the number of items read. You shouldn't compare the return value with EOF.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sander View Post
    The *scanf() functions return the number of items read. You shouldn't compare the return value with EOF.
    The *scanf() functions do ALSO return EOF when they reach end of file - I do agree that checking for EOF on it's own is insufficient to ensure that correct input was received under all conditions, it is perhaps acceptable to check for EOF only - particularly if the input is character/string data where the content of the file is acceptable whatever it is. If the input is numeric, then it's critical to check the return value (and correctly handle the situation) to avoid a lock-up when the input is invalid.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. processor fault!!
    By BMF in forum C Programming
    Replies: 3
    Last Post: 03-19-2005, 01:40 PM
  2. Using fscanf with a structure
    By daluu in forum C Programming
    Replies: 10
    Last Post: 10-11-2004, 02:32 PM
  3. fscanf and new lines
    By Countdown in forum C Programming
    Replies: 1
    Last Post: 05-29-2004, 11:50 PM
  4. Can I successively read lines using fscanf?
    By leontini in forum C Programming
    Replies: 2
    Last Post: 10-05-2003, 06:19 PM
  5. fscanf problem in C
    By kepler in forum C Programming
    Replies: 6
    Last Post: 09-30-2003, 07:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21