How to get a C++ perror(errno) message without printing it. - c++

If you use 'perror(errno)' it automatically prints the error to the standard error output-stream. Is there a way to get the value that would be printed without printing it?

You can use one of the strerror functions for that. They'll retrieve the error message without printing it.

Related

How to create an error so that GetLastError returns the error?

I've a function to convert GetLastError function to string to use in my project. But before deploying, I need to test in a sample program.
When I test it, it gets me no error, because the sample program doesn't have any error.
Can anyone help me how can I generate a couple of errors in std::errc, so that GetLastError throw them?
Call SetLastError() from the same thread with desired error code.
Take a look on the SetLastError documentation here

Using GetLastError when using Dos Commands in C++

So i have a bit of code that uses Dos Commands to try to rename a folder. So
system("rename C:\\Users\\me\\SecondDir NewDir);
So this tries to rename SecondDir to NewDir. There is already a folder at that location called NewDir so it should fail. And it does. Im then using GetLastError to get the error code returned to ensure the problem is what i expect it to be. But it only ever returns ERROR_NO_MORE_FILES. Which isnt the error i should be getting, which is ERROR_ALREADY_EXISTS. Im assuming this is something to do with using the system command?
EDIT: I just checked and i even get ERROR_NO_MORE_FILES returned when a command is successful.
GetLastError will not return a meaningful value except in the circumstances where it is documented to do so. This is not one of them - the values you are getting are irrelevant and intended for someone else.
To rename a file from C you should use the C runtime rename function not use system to invoke a rename utility.
GetLastError is only meaningful immediately after calling a Win32 function which is documented to set the thread Last Error using SetLastError. The C equivalent is errno, which applies to C functions.
The rename function returns -1 on failure and sets errno.
E.g.: http://msdn.microsoft.com/en-us/library/zw5t957f(v=VS.80).aspx

Failed writing body in libcurl

I am using libcurl for a small program that gets data from an input url. But i sometimes get an error from the pErrorBuffer like:
Failed writing to body (something != somethingelse)
What does this mean? I mean in what situation is this error created?
It means your write callback didn't return the same number of bytes as was passed into it!

SHLoadImageFile is returning error code 6

I am trying to load an image using SHLoadImageFile,but its returning invalid handle.
I am puzzled where the fault is.Because the path given to SHLoadImageFile is absolutely fine.
The question title currently is "SHLoadImage is returning error code 6". But SHLoadImage return is a handle, not an error code. So perhaps this means that you have called GetLastError?
If so, then error code 6 is defined in <winerror.h> as
//
// MessageId: ERROR_INVALID_HANDLE
//
// MessageText:
//
// The handle is invalid.
//
#define ERROR_INVALID_HANDLE 6L
The documentation does not however say anything about SHLoadImage setting the last error code, so perhaps this is something from earlier in the execution. Try using SetLastError to set the last error code to 0 before calling SHLoadImage.
For more specific advice you need to include more specific information, like showing the code with the "absolutely fine" path. If it really is, perhaps the file doesn't exist. Or is not a valid image.
Cheers & hth.,

How to tell flex and bison to stop processing input?

What is the best way to flex and bison to stop processing when an error is encountered. If I call yyerror, it does not stop scanning and parsing my file. While the input is syntactically correct, there is an user error, such as they tried to load the same file twice. Once I am out of flex/bison, then my program will return an error to the user and the program should keep running. I assume that throwing a C++ exception would probably break something?
YYABORT is the standard way of getting out; it causes yyparse to return immediately with a failure (1). You can then throw an exception or do whatever you want. You'll need to reset flex's input if you want to parse something else, but if you do, you can just call yyparse again and parsing will start over from the beginning.
YYACCEPT stop parse and return 0.