Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Logic_error when using vector obtain from sqlite select statement
    primarykey
    data
    text
    <p>I tried to run my C++ code and tried debuging for the past 1 hour. I keep checking, but not sure what I'm doing wrong?</p> <p>The code will not have issue If I key a wrong user/password, but if I key correctly, whereas it's enter the logincheck=actionvalue, it will throws error as shown below.</p> <p>Here is a look of my database select statement:</p> <pre><code>root@ubuntu:/home/baoky/version1.2/Assignment 2# sqlite3 abeserver.db SQLite version 3.7.9 2011-11-01 00:52:41 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite&gt; select * from abe_account; admin|Peter John|admin|password if (logincheck==actionvalue) </code></pre> <p>My code.cpp</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;signal.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;sys/un.h&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;fstream&gt; #include &lt;sqlite3.h&gt; #define DEFAULT_PROTOCOL 0 #ifndef AF_LOCAL #define AF_LOCAL AF_UNIX #endif #ifndef PF_LOCAL #define PF_LOCAL PF_UNIX #endif //g++ -o test test.cpp -lsqlite3 (sample compile with sqlite3) using namespace std; /* THIS IS SERVER CODE */ /* I WILL USE A TEMP FILE account.txt for basic login auth check */ int readLine (int fd, char* str) { int n; do /* Read characters until NULL or end-of-input */ { // ssize_t read (int fd, void *buf, size_t count); // if successful, read will: // a) stores data read into 'buf', and // b) returns the no. of bytes read // read returns zero if it reaches end-of-input n = read (fd, str, 1); /* Read one character */ } while (n &gt; 0 &amp;&amp; *str++ != 0); return (n &gt; 0); /* Return false if end-of-input */ } string readClient (int fd) { char str[2000]; while (readLine (fd, str)) /* Read lines until end-of-input */ return(string)str; /* return as string */ } std::vector&lt;std::string&gt; split(std::string const&amp; str, std::string const&amp; delimiters = "#") { std::vector&lt;std::string&gt; tokens; // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != lastPos) { // Found a token, add it to the vector. tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } return tokens; } std::vector&lt;std::string&gt; split(std::string const&amp; str, char const delimiter) { return split(str,std::string(1,delimiter)); } int main() { int serverFd; int clientFd; int serverLen; int clientLen; int counter; string action; string actionvalue; string receiveClient; string sendClient; string department; string sline; string logincheck; ifstream myfile; struct sockaddr* serverSockAddrPtr; struct sockaddr* clientSockAddrPtr; struct sockaddr_un serverAddress; struct sockaddr_un clientAddress; //for handle zombie //to ignore SIGCHLD(death of child signal).The zombies will not be seen. signal(SIGCHLD, SIG_IGN); //zombies(defunct processes ps command in Linux will show defunct entries) cout &lt;&lt; "" &lt;&lt; endl; cout &lt;&lt; "Running server program 'ABEServer' ...... " &lt;&lt; endl; cout &lt;&lt; "" &lt;&lt; endl; // SOCKET CREATION PART - SERVER serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL); /* Set domain type */ serverAddress.sun_family = AF_LOCAL; /* Set name */ strcpy (serverAddress.sun_path, "ABEServer"); /* GET SIZE OF Server Addres */ serverLen = sizeof serverAddress; /* GET SIZE OF Client Addres */ clientLen = sizeof clientAddress; /* Get Server Sock Address Pointer*/ serverSockAddrPtr = (struct sockaddr *) &amp;serverAddress; /* Get Client Sock Address Pointer*/ clientSockAddrPtr = (struct sockaddr *) &amp;clientAddress; /* Create file */ unlink("ABEServer"); bind (serverFd, serverSockAddrPtr , serverLen); /* listen for connection */ listen (serverFd,5); cout &lt;&lt; "Server started"; cout &lt;&lt; "" &lt;&lt; endl; // SOCKET CREATION END - SERVER while (1) /* Loop forever */ { /* Accept a client connection */ clientFd = accept (serverFd, clientSockAddrPtr, (socklen_t*) &amp;clientLen); if (fork () == 0) /* Create child to send client */ { while(1) { sendClient = ""; //read client input receiveClient = readClient(clientFd); vector&lt;string&gt; x = split(receiveClient, '#'); action = x[0]; actionvalue = x[1]; if(action=="auth") { logincheck = ""; counter = 0; department = ""; //default sendClient value sendClient = "fail login#Invalid username/password."; sqlite3 *db; sqlite3_stmt * stmt; std::vector&lt; std::vector &lt; std:: string &gt; &gt; result; for( int i = 0; i &lt; 4; i++ ) result.push_back(std::vector&lt; std::string &gt;()); if (sqlite3_open("abeserver.db", &amp;db) == SQLITE_OK) { sqlite3_prepare( db, "SELECT * from abe_account;", -1, &amp;stmt, NULL );//preparing the statement sqlite3_step( stmt );//executing the statement while( sqlite3_column_text( stmt, 0 ) ) { for( int i = 0; i &lt; 4; i++ ) result[i].push_back( std::string( (char *)sqlite3_column_text( stmt, i ) ) ); sqlite3_step( stmt ); counter++; } //close connection to db and finalize sql statement sqlite3_finalize(stmt); sqlite3_close(db); //username:password // using first record to check for ( int i = 0; i &lt; counter; i ++) { //result[column][row] logincheck = result[0][i] + ":" + result[3][i]; department = result[2][i]; } if (logincheck==actionvalue) { //send back in format of login done, login message, department level sendClient = "login done#Successfully Login"; break; } result.clear(); } }//end if auth write (clientFd, sendClient.c_str(), strlen (sendClient.c_str()) + 1); }//end while }//end if fork else { close (clientFd); /* Close the client descriptor */ }//end else }//end while outer return 0; } </code></pre> <p>Error Message:</p> <pre><code>Username &gt; admin Password &gt; password terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid Segmentation fault (core dumped) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload