I want to write the data I read from database to binary file. In the query I need to read a lot of data and some of them are group_concat. Now I want to write them into a binary file. The question is how can I define the size of them. At first I used put() but the result was wrong because put requires the type of char while row[] is char*. Then I tried write and defined the size by sizeof(int)*number of elements in group but it was still wrong. Please show me a way.
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);
group_concat?sizeof()try usingstrlen(row[0]) + 1to give the length of the string including the null terminator.