Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strol always returns error 122

Status
Not open for further replies.

bkelly13

Programmer
Aug 31, 2006
98
0
0
US
environment: Windows 7, 64 bit, Visual Studio 2008, C++
Code:
string t_string;
...
long long temp = strol( t_string.c_str(), NULL, 10 )
long error = GetLastError();
*return_value = (int) temp;

After finding that atoi returns 0 on error, I shifted to strol because it sets the system error value. However, if the string to be converted is not "0" then the error is always 122. Help files explaine this as: ERROR_INSUFFICIENT_BUFFER, The data area passed to a system call is too small.

I changed the return value from an int, then to a long, then to a long long with no effect.

In the above example, the string to be converted shows up in the debugger as "5508" or "16" or other such strings. Some not shown code isolates the string to be converted and puts it in t_string. The dugger shows that the conversion is correct even when GetLastError returns 122.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
GetLastError is for Windows functions. strtol is no a windows function. You should be using errno.
Code:
#include <errno.h>
...
string t_string;
...
long long temp = strol( t_string.c_str(), NULL, 10 )
long error = errno;
*return_value = (int) temp;
You can get the error strings using strerror(error)
 
Thank you, I will make some code changes.

Since the value from GetLastError() was not zero, I think that implies that there is an error. Perhaps I had best check the previous code.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Incidentally, strtol (not strol, of course;) returns long (not long long) value.
Use non-standard VC++ function _strtoi64 to get possible long long (64-bit) values correctly...
 
Hello ArkM,
When GetLastError() returned 122, ERROR_INSUFFICIENT_BUFFER, I tried the only thing I could think of, make the return variable longer. I with change to your recommended errorno() on Monday.
Thank you.

We need to know what a dragon is
before we study its anatomy.
(Bryan Kelly, 2010)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top