gpt4 book ai didi

c++ - 将从数据库读取的数据写入二进制文件

转载 作者:搜寻专家 更新时间:2023-10-30 23:45:45 25 4
gpt4 key购买 nike

我想将从数据库读取的数据写入二进制文件。在查询中我需要读取大量数据,其中一些是group_concat。现在我想将它们写入二进制文件。问题是我如何定义它们的大小。一开始我使用了put()但是结果是错误的因为put需要char的类型而row[]char*。然后我尝试了 write 并通过 sizeof(int)*number of elements in group 定义了大小,但它仍然是错误的。请告诉我一个方法。

MYSQL_RES* res = GetDBManager()->Query("select ls.time, count(ws.id) , group_concat(wc.id) , group_concat(ws.SIGNAL_STRENGTH) , ul.LONGITUDE , ul.LATITUDE , ul.ALTITUDE  from user_location_scan ls, wifi_scan ws, wifi_cell wc, user_location ul where ls.id = ws.user_scan and ws.wifi_cell = wc.id and ls.time = ul.time group by ls.id order by ls.id");
if (res == NULL) return false;

MYSQL_ROW row;

ofstream myFile("data.bin", ios::out | ios::binary | ios::app);

while (row = mysql_fetch_row(res)) {
myFile.write(row[0], sizeof(char));
myFile.write(row[1], sizeof(int));
myFile.write(row[2], sizeof(int)*atoi(row[1]));
myFile.write(row[3], sizeof(int)*atoi(row[1]));
myFile.write(row[4], sizeof(double));
myFile.write(row[5], sizeof(double));
myFile.write(row[6], sizeof(double));
//myFile.write(row[0], sizeof(row[0]));
//myFile.write(row[1], sizeof(row[1]));
//myFile.write(row[2], sizeof(row[2]));
//myFile.write(row[3], sizeof(row[3]));
//myFile.write(row[4], sizeof(row[4]));
//myFile.write(row[5], sizeof(row[5]));
//myFile.write(row[6], sizeof(row[6]));
//myFile.put((char)row[0]);
//myFile.put((char)row[1]);
//myFile.put((char)row[2]);
//myFile.put((char)row[3]);
//myFile.put((char)row[4]);
//myFile.put((char)row[5]);
//myFile.put((char)row[6]);
//myFile << row[0] << row[1] << row[2] << row[3] << row[4] << row[5] << row[6];

}
mysql_free_result(res);

最佳答案

MySql 函数 mysql_fetch_row() 返回一个空终止字符串数组(字符数组)。您可以使用 std::strlen() 获取这些以 null 结尾的字符数组的长度。

如果您想将所有内容作为字符串写到文件中,那么执行如下操作就足够了:

while((row = mysql_fetch_row(res)))
for(unsigned col = 0; col < 7; ++col)
myFile.write(row[col], std::strlen(row[col]) + 1);

如果您想将数字列写成二进制数(更难移植),那么您需要转换字符串并以不同方式写出值。

不太推荐是以二进制形式写出每条记录。这是非常不可移植的。

struct record
{
// choose better column names
char col_0[32]; // largest possible string
int col_1;
int col_2;
int col_3;
double col_4;
double col_5;
double col_6;
};

while((row = mysql_fetch_row(res)))
{
record r; // store our binary values here

//copy the string
std::strncpy(r.col_0, row[0], sizeof(r.col_0));

r.col_1 = std::atoi(row[1]); // convert the integers
r.col_2 = std::atoi(row[2]);
r.col_3 = std::atoi(row[3]);

r.col_4 = std::atof(row[4]); // convert the floats
r.col_5 = std::atof(row[5]);
r.col_6 = std::atof(row[6]);

// write the whole object out in binary (very non-portable)
myFile.write(reinterpret_cast<char*>(&r), sizeof(r));
}

并再次读取记录:

// reading back

record r; // store our binary values here

if(myFile.read(reinterpret_cast<char*>(&r), sizeof(r)))
{
// success, use the record here
std::cout << r.col_0 << '\n';
std::cout << r.col_1 << '\n';
std::cout << r.col_2 << '\n';
// ... etc
}

注意:使用二进制更复杂,更难正确,不可移植,除非绝对需要,否则通常应避免使用。

关于c++ - 将从数据库读取的数据写入二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28869795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com