is there a way to fetch the error into a text

Sep 12, 2010 at 11:58am
is there a way to fetch the error into a text
like for e.g in the php , we do mysql_query('select `what eva` from `wateva` where `wateva`='wateva'') or die (mysql_error());

well my point is that code (mysql_error()); will tell me what's wrong so i can fix it and the error result will change by the change of the error , if my words make sence lol

so is there a way in c++ to make it like save the errors in text or somthing.

thnx.
Sep 12, 2010 at 1:17pm
1
2
3
4
5
6
7
#include <string>

MYSQL mysql;

// ... stuff

std::string err = mysql_error(&mysql); // something like this should work 
Sep 12, 2010 at 1:35pm
@Galik

can you please explain more to me??

like MYSQL mysql; means what ??

and is there mysql_error in the c++, plus (&mysql) means what?

and what's wrong here
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <direct.h>
using namespace std;


int main(void)
{
CreateDirectory("syslog", NULL);

return 0;
}


thanks
Last edited on Sep 12, 2010 at 1:57pm
Sep 12, 2010 at 4:57pm
Errrrrrrrrrrrrrr no one knows :(
Sep 12, 2010 at 5:25pm
Sep 12, 2010 at 5:39pm
How you get the error depends on what API you're using, as every API handles errors differently.

Nobody gave you an answer because you didn't tell us what API you're using (and also it's only been 3 hours since you posted)

CreateDirectory looks like WinAPI, so assuming you're using WinAPI:


WinAPI doesn't have the easiest to use error system, but it is possible to get error strings. Whenever an error occurs you can get the error with GetLastError() http://msdn.microsoft.com/en-us/library/ms679360(v=VS.85).aspx

This returns an error code (not a human readable string). So convert this to a string, you can use FormatMessage: http://msdn.microsoft.com/en-us/library/ms679351(v=VS.85).aspx

There's an example here: http://msdn.microsoft.com/en-us/library/ms680582(v=VS.85).aspx


But as you can see it's not very simple. Here's a copy/pastable function you can use for simplicity: (but please note I didn't test it so I can't say with 100% certainty that it will work, but it should):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string GetLastErrorString()
{
    string ret;
    char* buf;

    FormatMessageA(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        GetLastError(),
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        &buf,
        0, NULL );

    ret = buf;
    LocalFree(buf);

    return ret;
}


Put that function in your program, then you can print errors like this:

1
2
3
4
5
int main()
{
  if(!CreateDirectory("syslog",NULL))
    cerr << "An error occured:  " << GetLastErrorString();
}



EDIT: doh, too slow XD
Last edited on Sep 12, 2010 at 5:40pm
Sep 12, 2010 at 5:39pm
Scripting language like PHP is very different with C++. Usually when functions fail in C++ it returns false or -1. There is no string representation of those like in PHP. Though redirecting the output of cout, cin, cerr is also possible in C++
Topic archived. No new replies allowed.