[英]Anyone know how I can fix this C++ for trying to print the rows returning from a MySQL Database into a Text File(.txt)
我是C ++的新手,所以不確定。
該應用程序的基本目的是允許用戶輸入SQL查詢,然后將結果行從數據庫返回到text(.txt)文件。
這是之前的部分,還有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;
這是不斷導致程序崩潰的代碼段,但我無法弄清楚是什么導致程序崩潰
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";
}
row = mysql_fetch_row(res);
到目前為止,一切看起來都還不錯,但是您將row
視為字符串:
theSaveFile<<""+row+" \t\t";
那是錯誤的。 您尾隨的+" \\t\\t"
和奇怪的""+
(不要這樣做; C ++不是JavaScript)可能正在執行越界指針算術。 如果您正確完成了流式傳輸:
theSaveFile << row << " \t\t";
那么我想您的編譯器會更好地警告您該row
不能用這種方式處理。
手冊中有一個有關如何使用MYSQL_ROW
。 提示:這是一個數組; 結果集中每一列 (或“字段”)的元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.