Reputation: 147
I'm very new to C++ so I'm not sure.
The Basic aim of the application is to allow the user to enter an SQL Query and then return the rows of results from the database to a text(.txt) file.
Here is part before and the variables for the int Main()
#include <iostream>
#include <string>
#include <windows.h>
#include <mysql.h>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
MYSQL* conn;
MYSQL_ROW row;
MYSQL_RES *res;
unsigned int num_rows;
unsigned int ii;
int qstate;
This is the code segment that keeps causing the problem of making the program crash but I just cant figure out whats causing it to crash
if(!qstate)
{
res = mysql_store_result(conn);
cout<<"Enter File Name: "<<endl;
std:string FileName;
std::getline(cin, FileName);
string FN = ""+FileName+".txt";
const char* fileN = FN.c_str();
ofstream theSaveFile(fileN, ios::app);
num_rows = mysql_num_rows(res);
row = mysql_fetch_row(res);
for(ii = 0; ii < num_rows; ii++)
{
if(!theSaveFile)
{
cout<<"Printing to the file failed"
<<endl;
exit(1);
}
theSaveFile<<""+row+" \t\t";
}
Upvotes: 0
Views: 244
Reputation: 385194
row = mysql_fetch_row(res);
Everything up until now looks ok, but then you treat row
as a string:
theSaveFile<<""+row+" \t\t";
That's just wrong. Your trailing +" \t\t"
and your strange ""+
(don't do this; C++ is not JavaScript) are probably performing out-of-bounds pointer arithmetic. If you'd done streaming properly:
theSaveFile << row << " \t\t";
then I imagine your compiler would be better placed to warn you that row
cannot be treated this way.
There's an example in the manual of how to use MYSQL_ROW
. Hint: it's an array; one element for each column (or "field") in your resultset.
Upvotes: 2