-
August 26th, 2016, 03:30 PM
#1
[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.
if user enter more than 50 characters then exe will crash. I would like to use like a string where no size initialize is required. Any idea ?
-
August 26th, 2016, 04:06 PM
#2
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 26th, 2016, 04:38 PM
#3
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 ?
-
August 26th, 2016, 05:12 PM
#4
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 ?
-
August 26th, 2016, 06:35 PM
#5
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.
-
August 26th, 2016, 07:23 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|