I want to get int from cstring.
Here is Code.
CStringArray paramArray;
paramArray.Add((LPCSTR)"5");
paramArray.Add((LPCTSTR)"151");
pvarArguments = new CComVariant[2];
pvarArguments[0] = (LPCTSTR)paramArray[1];
CString str;
str = (CStringA)pvarArguments[0];
int nlen = _wtoi(str.GetBuffer());
When I run my program, I always get value 0, and I can't understand why it is.
Please help me.
From TFM (emphasis mine):
Each function returns the int value produced by interpreting the input
characters as a number. The return value is 0 for atoi and _wtoi, if
the input cannot be converted to a value of that type.
Print the string or examine it using a debugger. There may be invalid (including unprintable) characters in the string.
It's hard to tell even what you are trying to do. You do know that C++ arrays are 0-based, right? I ask because this line of code:
pvarArguments[0] = (LPCTSTR)paramArray[1];
is totally messed up. I don't understand why it's not throwing an exception when trying to index an element in a CStringArray that is equal to the count of elements. You can only index to count-1 ==>> which in this case is "0".
Your pvarArguments[0] will have junk in it--I have no idea why an exception wasn't thrown.
If you want to get a different type out of a variant, you can use VariantChangeType() and not mess with wcstoi or atoi. It will give an error code if it fails instead of just returning 0. If you are hell bent on using wcstoi or atoi on a CString, use _tstoi() which works whether you nave UNICODE defined or not.
CStringA implies ANSI string type which would require atoi not _wtoi.
I suggest:
CString str(pvarArguments[0]);
int nlen = atoi(str.GetString());
GetBuffer() is not ideal because you must remember to later ReleaseBuffer().
EDIT: In light of the new information, try this:
paramArray.Add(L"5");
paramArray.Add(L"151");
The L macro makes the string wchar_t aware. If L doesn't work try _T instead. And then use _wtoi or _tstoi.
CStringArray paramArray;
paramArray.Add(_T("5"));
paramArray.Add(_T("151"));
CComVariant *pvarArguments = new CComVariant[2];
pvarArguments[0] = (LPCTSTR)paramArray[1];
CString str;
str = pvarArguments[0].bstrVal;
int nlen = _ttoi(LPCTSTR(str));
per suggestion above, this compiles for me:
rs->GetFieldValueString(0).Left(1) == sBoroCode
&& (_ttoi(LPCTSTR(sLowHouseNo)) % 2) == (_ttoi(LPCTSTR(rs->GetFieldValueString(2))) % 2)
Related
I was looking at a project and came across the following code and am unable to figure out what the sprintf is doing in this context and was hoping someone might be able to help me figure it out.
char storage[64];
int loc = 0;
int size = 35;
sprintf(storage+(loc),"A"); //Don't know what this does
loc+=1;
sprintf(storage+(loc),"%i", size); //Don't know what this does
loc+=4;
sprintf(storage+(loc), "%i", start); //Don't know what this does
start += size;
loc += 3;
The code later does the following in another part
string value;
int actVal;
int index = 0;
for(int j = index+1; j < index+4; j++)
{
value += storage[j];
}
istringstream iss;
iss.str(value);
iss >> actVal; //Don't understand how this now contains size
The examples I have seen online regarding sprintf never covered that the above code was possible, but the program executes fine. I just can't figure out how the "+loc" affects storage in this instance and how the values would be saved/stored. Any help would be appreciated.
Ugly code! Regardless, for the first part, storage+(loc) == &storage[loc]. You end up with a string "A35\0<unknown_value>1234\0", assuming start = 1234, or in long form:
sprintf(&storage[0],"A");
sprintf(&storage[1],"%i", size);
sprintf(&storage[5], "%i", start);
For the second part, assuming we have the "A35\0<unknown_value>1234\0" above, we get:
value += '3';
value += '5';
value += '\0';
value += '<unknown_value>'; // This might technically be undefined behaviour
So now value = "35". [1]
iss.str(value);
iss >> actVal;
This turns the string into an input stream and reads out the first string representing an integer, "35", and converts it into an integer, giving us basically actVal = atoi(value.c_str());.
Finally, according to this page, yes, reading an uninitialised ("indeterminate value" is the official term) array element is undefined behaviour thus should be avoided.
[1] Note that in a usual implementation, there is a theoretical 10/256 chance that the <unknown_value> could contain an ASCII digit, so value could end up being between 350 and 359, which is obviously not a good outcome and is why one shouldn't ignore undefined behaviour.
The function sprintf() works just like printf(), except the result is not printed in stdout, rather it is store in a string variable. I suggest you read the sprintf() man page carefully:
https://linux.die.net/man/3/sprintf
Even if you are not on a Linux, that function is pretty much similar across different platforms, be it Windows, Mac or other animals. That said, this piece of code you have presented seems to be unnecessarily complicated.
The first part could be written as:
sprintf(storage,"A %i %i", size, start);
For a similar-but-not-equal result, but then again, it all depends on what exactly the original programmer intended this storage area to hold. As Ken pointed out, there are some undefined bytes and behaviors coming from this code as-is.
From the standard:
int sprintf ( char * str, const char * format, ... );
Write formatted data to string
Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.
sprintf(storage+(loc),"A");
writes "A" into a buffer called storage. The storage+(loc) is pointer arithmetic. You're specifying which index of the char array you're writing into. So, storage = "A".
sprintf(storage+(loc),"%i", size);
Here you're writing size into storage[1]. Now storage = "A35\0", loc = 1, and so on.
Your final value of storage = "A35\0<garbage><value of start>\0"
actVal: Don't understand how this now contains size
The for loop goes through storage[1] through storage[5], and builds up value using the contents of storage. value contains the string "35\0<garbage>", and iss.str(value) strips it down to "35\0".
iss >> actVal
If you have come across std::cin, it's the same concept. The first string containing an integer value is written into actVal.
I am asking for information to save onto the database like their username and stuff, you can use up to 10 characters perfectly fine, the wstring holds it and I can use it how I like, but if a user were to type 11 characters it would all of a sudden say "Error reading characters of strings".
At first I thought I didn't have a big enough space for GetWindowText so I pumped that up, I didn't know if you can change the wstring capacity so that is why I am asking here.
Why is wstring only working with 10 or less characters? Thanks!
case WM_COMMAND: {
switch (LOWORD(wParam))
{
case IDB_REGISTERACCOUNT: {
std::wstringstream SQLStatementStream;
std::wstring SQLUsername;
std::wstring SQLPassword;
//Get user information than store in wide Strings
GetWindowText(hUserNameRegister, &SQLUsername[0], 50);
GetWindowText(hPasswordRegister, &SQLPassword[0], 50);
std::wstring SQLStatement = SQLStatementStream.str();
break;
}
}
break;
}
Your code is essentially saying, "I am giving you the address of the first character of this string, start writing to it indiscriminately." This is going to be a bad time. What you need to do is first allocate an actual buffer to use to store the result, then if you wanted in the wstring, you can use the wstring's '=' operator to do the proper assignment.
something like:
WCHAR temp[50];
GetWindowText(hUserNameRegister, temp, 50);
SQLUsername = temp
You write past the end of your string. This causes undefined behaviour.
Only use &x[0] on a standard library string when you are in read-only mode, or if you are writing but not going to change the length of the string.
You should have written:
wchar_t SQLUsername[50] = {};
GetWindowTextW(hUserNameRegister, SQLUsername, 50);
Then you can convert to wstring if you wanted with std::wstring wsSQLUsername = SQLUsername; - however your code never actually uses your wstrings before they go out of scope so perhaps you have some other misconceptions too.
So I have two hex strings - "3b101c091d53320c000910" and "071d154502010a04000419". When I use strtol() on them I get same value for both strings.
I tried the following code-
string t1="3b101c091d53320c000910";
long int hext1=strtol(t1.c_str(),0,16);
string t2="071d154502010a04000419";
long int hext2=strtol(t2.c_str(),0,16);
cout<<hext1<<endl;
cout<<hext2<<endl;
Both are giving me same value: 9223372036854775807.
I dont know how strtol() works exactly since I am new to C++ but it's giving me same value for two different hex strings. Why?
You should start by reading the manual page. It's returning LONG_MAX since you're input is too large to fit in a long.
Also, strtol() is a very C way of doing things, and you're programming in C++.
You're not using strtol correctly. You should set errno to
0 before calling it, and check that it is still 0 after;
otherwise, it will contain an error code (which can be displayed
using strerror). Also, you should pass it the address of
a char const*, so that you can ensure that it has processed
the entire string (otherwise, "abc" will return 0, without an
error):
errno = 0;
char const* end;
long hext1 = strtol( t1.c_str(), &end, 16 );
if ( errno != 0 || *end != '\0' ) {
// Error occured.
}
I'm trying to understand why a segmentation fault (SIGSEGV) occurs during the execution of this piece of code. This error occurs when testing the condition specified in the while instruction, but it does not occur at the first iteration, but at the second iteration.
LPTSTR arrayStr[STR_COUNT];
LPTSTR inputStr;
LPTSTR str;
// calls a function from external library
// in order to set the inputStr string
set_input_str(param1, (char*)&inputStr, param3);
str = inputStr;
while( *str != '\0' )
{
if( debug )
printf("String[%d]: %s\n", i, (char*)str);
arrayStr[i] = str;
str = str + strlen((char*)str) + 1;
i++;
}
After reading this answer, I have done some research on the internet and found this article, so I tried to modify the above code, using this piece of code read in this article (see below). However, this change did not solve the problem.
for (LPTSTR pszz = pszzStart; *pszz; pszz += lstrlen(pszz) + 1) {
... do something with pszz ...
}
As assumed in this answer, it seems that the code expects double null terminated arrays of string. Therefore, I wonder how I could check the contents of the inputStr string, in order to check if it actually contains only one null terminator char.
NOTE: the number of characters in the string printed from printf instruction is twice the value returned by the lstrlen(str) function call at the first iteration.
OK, now that you've included the rest of the code it is clear that it is indeed meant to parse a set of consecutive strings. The problem is that you're mixing narrow and wide string types. All you need to do to fix it is change the variable definitions (and remove the casts):
char *arrayStr[STR_COUNT];
char *inputStr;
char *str;
// calls a function from external library
// in order to set the inputStr string
set_input_str(param1, &inputStr, param3);
str = inputStr;
while( *str != '\0' )
{
if( debug )
printf("String[%d]: %s\n", i, str);
arrayStr[i] = str;
str = str + strlen(str) + 1;
i++;
}
Specifically, the issue was occurring on this line:
while( *str != '\0' )
since you hadn't cast str to char * the comparison was looking for a wide nul rather than a narrow nul.
str = str + strlen(str) + 1;
You go out of bounds, change to
str = str + 1;
or simply:
str++;
Of course you are inconsistently using TSTR and strlen, the latter assuming TCHAR = char
In any case, strlen returns the length of the string, which is the number of characters it contains not including the nul character.
Your arithmetic is out by one but you know you have to add one to the length of the string when you allocate the buffer.
Here however you are starting at position 0 and adding the length which means you are at position len which is the length of the string. Now the string runs from offset 0 to offset len - 1 and offset len holds the null character. Offset len + 1 is out of bounds.
Sometimes you might get away with reading it, if there is extra padding, but it is undefined behaviour and here you got a segfault.
This looks to me like code that expects double null terminated arrays of strings. I suspect that you are passing a single null terminated string.
So you are using something like this:
const char* inputStr = "blah";
but the code expects two null terminators. Such as:
const char* inputStr = "blah\0";
or perhaps an input value with multiple strings:
const char* inputStr = "foo\0bar\0";
Note that these final two strings are indeed double null terminated. Although only one null terminator is written explicitly at the end of the string, the compiler adds another one implicitly.
Your question edit throws a new spanner in the works? The cast in
strlen((char*)str)
is massively dubious. If you need to cast then the cast must be wrong. One wonders what LPTSTR expands to for you. Presumably it expands to wchar_t* since you added that cast to make the code compile. And if so, then the cast does no good. You are lying to the compiler (str is not char*) and lying to the compiler never ends well.
The reason for the segmentation fault is already given by Alter's answer. However, I'd like to add that the usual style of parsing a C-style string is more elegant and less verbose
while (char ch = *str++)
{
// other instructions
// ...
}
The scope of ch is only within in the body of the loop.
Aside: Either tag the question as C or C++ but not both, they're different languages.
I've just finished C++ The Complete Reference and I'm creating a few test classes to learn the language better. The first class I've made mimics the Java StringBuilder class and the method that returns the string is as follows:
char *copy = new char[index];
register int i;
for(i = 0; i <= index; i++) {
*(copy + i) = *(stringArray + i);
} //f
return copy;
stringArray is the array that holds the string that is being built, index represents the amount of characters that have been entered.
When the string returns there is some junk after it, such as if the string created is abcd the result is abcd with 10 random characters after it. Where is this junk coming from? If you need to see more of the code please ask.
You need to null terminate the string. That null character tells the computer when when string ends.
char * copy = new char[ length + 1];
for(int i = 0; i < length; ++i) copy[i] = stringArray[i];
copy[length] = 0; //null terminate it
Just a few things. Declare the int variable in the tighest scope possible for good practice. It is good practice so that unneeded scope wont' be populate, also easier on debugging and kepping track. And drop the 'register' keyword, let the compiler determine what needs to be optimized. Although the register keyword just hints, unless your code is really tight on performance, ignore stuff like that for now.
Does index contain the length of the string you're copying from including the terminating null character? If it doesn't then that's your problem right there.
If stringArrary isn't null-terminated - which can be fine under some circumstances - you need to ensure that you append the null terminator to the string you return, otherwise you don't have a valid C string and as you already noticed, you get a "bunch of junk characters" after it. That's actually a buffer overflow, so it's not quite as harmless as it seems.
You'll have to amend your code as follows:
char *copy = new char[index + 1];
And after the copy loop, you need to add the following line of code to add the null terminator:
copy[index] = '\0';
In general I would recommend to copy the string out of stringArray using strncpy() instead of hand rolling the loop - in most cases strncpy is optimized by the library vendor for maximum performance. You'll still have to ensure that the resulting string is null terminated, though.