dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] char [] or char [large size] question

  1. #1
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    [RESOLVED] char [] or char [large size] question

    Hello
    I have a very simple question yet can't find answer. I need to store key from user input in a char but don't know how to set correct size without causing buffer errors.
    Code:
    char key[50]
    if user enter more than 50 characters then exe will crash. I would like to use like a string
    Code:
    std::string key
    where no size initialize is required. Any idea ?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,922

    Re: char [] or char [large size] question

    What's wrong with using std::string? Either stream extraction using >> or getline() can be used to obtain user input to std::string. If you need to access as char* (for passing to a function etc) then use .c_str().

    See
    http://www.cplusplus.com/reference/s...tring/getline/
    http://www.cplusplus.com/reference/string/string/c_str/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  3. #3
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: char [] or char [large size] question

    I need to format it with wsprintfA also code is running in another space so i'm limited to char. I'm thinking of something like:
    Code:
    				char userinput[256];
    				char buffer[] = "";
    				wsprintfA(buffer, "input&log=%s", userinput);
    I need to format a text that sometime is larger than 256. I don't know how to format a std::string without using char. Is there any way ?

  4. #4
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: char [] or char [large size] question

    Code:
    char wnd_title[256];
    HWND hwnd = GetForegroundWindow();
    GetWindowTextA(hwnd, wnd_title, 256);
    Take this simple example. What if window title has more than 256 characters ? App will crash.
    My problem is something similar.
    Any idea how to make this work ?

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,716

    Re: char [] or char [large size] question

    You can use GetWindowTextLength() to get the length of the window text (or a larger value).
    See the documention.

  6. #6
    Join Date
    Sep 2014
    Location
    Las Vegas, NV
    Posts
    85

    Re: char [] or char [large size] question

    thanks for help guys! i resolved it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)