I'm very new to C++ so I'm not sure.
我对C ++很新,所以我不确定。
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.
该应用程序的基本目标是允许用户输入SQL查询,然后将数据库中的结果行返回到文本(.txt)文件。
Here is part before and the variables for the int Main()
这是前面的部分和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";
}
1 个解决方案
#1
2
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:
那是错的。您的尾随+“\ t \ t”和您的奇怪“”+(不要这样做; C ++不是JavaScript)可能正在执行越界指针算法。如果您已正确完成流式传输:
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.
手册中有一个如何使用MYSQL_ROW的例子。提示:这是一个数组;结果集中每列(或“字段”)的一个元素。
#1
2
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:
那是错的。您的尾随+“\ t \ t”和您的奇怪“”+(不要这样做; C ++不是JavaScript)可能正在执行越界指针算法。如果您已正确完成流式传输:
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.
手册中有一个如何使用MYSQL_ROW的例子。提示:这是一个数组;结果集中每列(或“字段”)的一个元素。