Thread: Is this good C++ code? And should the tests on swscanf work.

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4,183

    Is this good C++ code? And should the tests on swscanf work.

    Code:
    #include <iostream>
    #include <wchar.h>
    
    int main()
    {
        bool failed = false;
        int i = 0;
        wchar_t wstr[20];
    
        std::wstring wide_input(L"42 test");
    
        std::wstring wide_result(L"test");
    
        std::cout << "Testing swscanf function!" << std::endl;
        swscanf(wide_input.c_str(), L"%d %s", &i, &wstr);
    
        if (i != 42){
          failed = true;
        }
    
        if (wstr != wide_result){
          failed = true;
        }
    
        if (failed == true){
            return -1;
        }
    
        std::cerr <<  "Passed all of the tests";
        return 0;
    }
    I am trying to use cppunit to test the wxWidgets building under Windows 10 32 bit MSys2 (MSys2 is a fork of CygWin).

    And, the cppunit tests segfaults; where it segfaults suggest a broken swscanf support function called __mingw_swformat in the crt library.

    The MSys2 GCC crt library is based on MinGW-64 crt library with only slight patches.

    Edit: The above code crashes when compiled with the MSys2 GCC (Rev1, Built by MSYS2 project) 6.3.0.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're forgetting that an array is already interpreted as a pointer to the first element, so you shouldn't have the ampersand before wstr.

    Also, to read a wide string you need to use "%ls". (That's a lowercase L.)

    As for good C++, people usually forgo the scanf function in favour of stringstream.
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    #include <cwchar>
    
    using std::wcout;
    
    int main() {
        int i = 0;
        wchar_t wstr[20] = L"default";
        std::wstring wide_input(L"42 test"); 
        std::wstring wide_result(L"test");
    
        wcout << L"Testing swscanf function\n";
    
        swscanf(wide_input.c_str(), L"%d %ls", &i, wstr);
     
        if (i != 42) {
          wcout << L"i != 42\n";
          return 1;
        }
     
        if (wstr != wide_result){
          wcout << L"wstr != wide_result\n";
          return 1;
        }
     
        wcout <<  L"swscanf passed all of the tests\n";
    
        wcout << L"Testing wstringstream\n";
    
        i = 0;
        wstr[0] = L'\0';
    
        std::wstringstream wss(wide_input);
        wss >> i;
        wss >> wstr;
    
        if (i != 42) {
          wcout << L"i != 42\n";
          return 1;
        }
     
        if (wstr != wide_result){
          wcout << L"wstr != wide_result\n";
          return 1;
        }
     
        wcout <<  L"wstringstream passed all of the tests\n";
    
        return 0;
    }
    Last edited by algorism; 02-14-2017 at 07:11 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Thank you algorism.

    I can now see if wstringstream passes.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why cin.good does not work in this code?
    By fanli in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2016, 02:57 AM
  2. Does this example code work good?
    By Todd_65536 in forum C Programming
    Replies: 2
    Last Post: 11-25-2013, 03:11 PM
  3. Is this code good?
    By AsmLover in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 04:00 PM
  4. does anyone have good code????
    By K&J in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2001, 10:32 AM
  5. I know OpenGL...now i want a good project 2 work on...
    By Zeeshan Zia in forum Game Programming
    Replies: 2
    Last Post: 10-20-2001, 12:18 PM

Tags for this Thread