Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. swscanf_s White Space Help

swscanf_s White Space Help

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jesuaw
    wrote on last edited by
    #1

    Hello, A wchar_t array contains two sentences inside which I need to save in two separate variables.

    wchar_t str[] = L"'This is the first sentence' 'This is the second sentence'";

    I want to split in a way that first sentence and second sentence equals to these:

    wchar_t first_sentence[MAX_PATH]=L"'This is the first sentence'";
    wchar_t second_sentence[MAX_PATH]=L"'This is the second sentence'";

    I am using below code but it does not work as expected.

    #include "stdafx.h"
    #include <wchar.h>
    #include <windows.h>
    #include <stdio.h>

    int _tmain(int argc, _TCHAR* argv[])
    {
    wchar_t str[] = L"'This is the first sentence' 'This is the second sentence'";

    wchar\_t first\_sentence\[MAX\_PATH\];
    wchar\_t second\_sentence\[MAX\_PATH\];
    swscanf\_s(str, L"'%s' '%s'", first\_sentence, \_countof(first\_sentence), second\_sentence, \_countof(second\_sentence));
    
    printf("first\_sentence=%ls\\nsecond\_sentence=%ls\\n", first\_sentence, second\_sentence);
    
    system("PAUSE");
    return 0;
    

    }

    As far as I understand swscanf_s is pausing when it encountes the first white space. How am I suppose to get whole string between single quotes with swscanf_s please?

    K 1 Reply Last reply
    0
    • J Jesuaw

      Hello, A wchar_t array contains two sentences inside which I need to save in two separate variables.

      wchar_t str[] = L"'This is the first sentence' 'This is the second sentence'";

      I want to split in a way that first sentence and second sentence equals to these:

      wchar_t first_sentence[MAX_PATH]=L"'This is the first sentence'";
      wchar_t second_sentence[MAX_PATH]=L"'This is the second sentence'";

      I am using below code but it does not work as expected.

      #include "stdafx.h"
      #include <wchar.h>
      #include <windows.h>
      #include <stdio.h>

      int _tmain(int argc, _TCHAR* argv[])
      {
      wchar_t str[] = L"'This is the first sentence' 'This is the second sentence'";

      wchar\_t first\_sentence\[MAX\_PATH\];
      wchar\_t second\_sentence\[MAX\_PATH\];
      swscanf\_s(str, L"'%s' '%s'", first\_sentence, \_countof(first\_sentence), second\_sentence, \_countof(second\_sentence));
      
      printf("first\_sentence=%ls\\nsecond\_sentence=%ls\\n", first\_sentence, second\_sentence);
      
      system("PAUSE");
      return 0;
      

      }

      As far as I understand swscanf_s is pausing when it encountes the first white space. How am I suppose to get whole string between single quotes with swscanf_s please?

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      the format specifier you want is %[^'] i.e. all chars that are not a single quote.

      J 1 Reply Last reply
      0
      • K k5054

        the format specifier you want is %[^'] i.e. all chars that are not a single quote.

        J Offline
        J Offline
        Jesuaw
        wrote on last edited by
        #3

        Thank you very much for your help. Works great. One more question please. How can I select all the characters till to a specific word? Say, I want to select all characters till "the" word? I tried

        %[^(the)]

        but to no avail. Cheers

        K 1 Reply Last reply
        0
        • J Jesuaw

          Thank you very much for your help. Works great. One more question please. How can I select all the characters till to a specific word? Say, I want to select all characters till "the" word? I tried

          %[^(the)]

          but to no avail. Cheers

          K Offline
          K Offline
          k5054
          wrote on last edited by
          #4

          Even if that worked, it probably wouldn't do what you wanted - e.g. if the input text was "a small withered man with the axe", you'd get "a small wi" copied to your output variable. Depending on what you're trying to do, maybe regex() might be a better tool in this case, though I'm not sure about regex() and wide strings.

          J 1 Reply Last reply
          0
          • K k5054

            Even if that worked, it probably wouldn't do what you wanted - e.g. if the input text was "a small withered man with the axe", you'd get "a small wi" copied to your output variable. Depending on what you're trying to do, maybe regex() might be a better tool in this case, though I'm not sure about regex() and wide strings.

            J Offline
            J Offline
            Jesuaw
            wrote on last edited by
            #5

            I see. Thanks again for your help. Cheers

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups