Thread: Variable Size and Inititation Help

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    26

    Variable Size and Inititation Help

    Hello,

    I need to read commandline arguments for my VS C++ program:
    Code:
        wchar_t cmdString[1000];
        wcscpy_s(cmdString, (wchar_t *)GetCommandLine());
    I have set cmdString array length to be 1000 but I am not sure how long command line length might be.

    Is there any solution that can let me assign on the fly like below:
    Code:
    wchar_t cmdString[] = (wchar_t *)GetCommandLine()
    What is the right approach for initializing such kind of variables that their length is unknown?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by JessicaS View Post
    What is the right approach for initializingsuch kind of variables that their length is unknown?
    If you use VC++, you can just use wmain() and be fine with properly tokenized command line. If you really need to use GetCommandLineW(), then you can pass the result to CommandLineToArgvW(). Unfortunately, there is no standard C++ way to get args in anything other than char*, so no matter what you do, you must use platform-specific stuff.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by JessicaS View Post
    Is there any solution that can let me assign on the fly like below:
    Code:
    wchar_t cmdString[] = (wchar_t *)GetCommandLine()
    What is the right approach for initializing such kind of variables that their length is unknown?
    As for this question, the answer is that yes, you can. You should simply use std::string/std::wstring:

    std::string Arguments = GetCommandLineA();
    or
    std::wstring Arguments = GetCommandLineW();
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2016
    Posts
    26
    Thanks Elysia.

    I should definetely quit using wchar_t apparently.

    Actually I have copied those codes from web, so trying to continue their use. But apparently there are more easy ways.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable size limits
    By rogster001 in forum C Programming
    Replies: 2
    Last Post: 09-01-2009, 06:51 AM
  2. How do i know the size of variable pointer
    By unikgila in forum C Programming
    Replies: 10
    Last Post: 10-28-2008, 10:08 AM
  3. array of a variable size
    By dougwilliams in forum C Programming
    Replies: 3
    Last Post: 11-17-2007, 11:55 PM
  4. help on variable size array
    By 1qaz1234 in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2005, 12:02 PM
  5. Variable Size
    By Gr3g in forum C++ Programming
    Replies: 13
    Last Post: 04-17-2002, 11:09 AM