用Ubuntu20.04.5的vscode运行一个读取文件中的数据进行搜索的代码,报错:
writing into constant object (argument 3) [-Wformat=]
错误如图,我想把txt文件中的string类型数据读取出来,写入的时候就是用c_str()函数写入的,并没有出错,读的时候也想这样读取,但是却出错了,网上找了一圈,没有找到有效的解决办法,c++有可以解决的办法,但是实现特定的功能需要用c语言,所以只能用c语言实现。有人知道如何解决这种问题吗?按照@快乐鹦鹉 这位的方法成功解决了这个问题,但是又产生了新的问题,由于在递归函数里定义了许多char类型局部变量产生了新的问题如下:
这应该是由于递归导致的栈溢出吧,网上百度了一些方法,也尝试把这些字符数组设为全局变量,然后每次在函数里重新赋值也还是报错,不过仍然没有读取完2000个文件,有人知道有效的解决办法吗?
代码头文件define_document_encryption.h如下:
/***
* This code .h file define size and struct used.
*/
#define FILE_SIZE 2000//支持的文件数量,2000个文件的信息
#define DICT_SIZE 60//支持的数据长度
#define FILE_PATH1 "./source/data stem Bloom filter2001/workclass/line0.txt" //非敏感属性辅助匹配信息布隆过滤器的位置
#define FILE_PATH2 "./source/data stem Bloom filter2001/educ/line0.txt" //敏感属性辅助匹配信息布隆过滤器的位置
#define FILE_PATH3 "./source/data stem Bloom filter2001/maritalstatu/line0.txt"//非敏感属性辅助匹配信息布隆过滤器的位置
#define FILE_PATH4 "./source/data stem Bloom filter2001/occup/line0.txt" //敏感属性辅助匹配信息布隆过滤器的位置
#define FILE_PATH5 "./source/data stem Bloom filter2001/relationship/line0.txt"//非敏感属性辅助匹配信息布隆过滤器的位置
#define FILE_PATH6 "./source/data stem Bloom filter2001/race/line0.txt" //敏感属性辅助匹配信息布隆过滤器的位置
#define FILE_PATH7 "./source/data stem Bloom filter2001/gend/line0.txt" //非敏感属性辅助匹配信息布隆过滤器的位置
#define FILE_PATH8 "./source/data stem Bloom filter2001/countri/line0.txt" //敏感属性辅助匹配信息布隆过滤器的位置
// struct of node
typedef struct record_entry {//tree_node
int lid; // node ID
string att1;
int B_att1[DICT_SIZE];
string val1;
int B_val1[DICT_SIZE];
unsigned char *sig1_1;
string att2;
int B_att2[DICT_SIZE];
string val2;
int B_val2[DICT_SIZE];
unsigned char *sig1_2;
string att3;
int B_att3[DICT_SIZE];
string val3;
int B_val3[DICT_SIZE];
unsigned char *sig1_3;
string att4;
int B_att4[DICT_SIZE];
string val4;
int B_val4[DICT_SIZE];
unsigned char *sig1_4;
string att5;
int B_att5[DICT_SIZE];
string val5;
int B_val5[DICT_SIZE];
unsigned char *sig1_5;
string att6;
int B_att6[DICT_SIZE];
string val6;
int B_val6[DICT_SIZE];
unsigned char *sig1_6;
string att7;
int B_att7[DICT_SIZE];
string val7;
int B_val7[DICT_SIZE];
unsigned char *sig1_7;
string att8;
int B_att8[DICT_SIZE];
string val8;
int B_val8[DICT_SIZE];
unsigned char *sig1_8;
struct record_entry *P; // pointer to next record
} Record;//Node
/*注意上述路径要根据自己文件的放置情况进行修改,不然可能会出现“ERROR: GDB exited unexpectedly. Debugging will now abort.“
或者“Aborted (core dumped)”的错误*/
运行出错的程序condition_search_edocument.cpp如下:
//在文件中进行条件搜索
#include <cstdio>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <ctime>
#include <fstream>
#include <iostream>
#include <math.h>
#include <openssl/md5.h>
#include <openssl/rand.h>
#include <sstream>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <vector>
#include <openssl/sha.h>
#include <random>
#include <cstdint>
#include <limits.h>
#include <algorithm>
#include "/usr/local/include/pbc/pbc.h"
#include "/usr/local/include/pbc/pbc_test.h"
using namespace std;
#include "define_document_encryption.h"
//理想结果输出样式:文件xxx中,符合条件的属性字段为xxx,条件关键词验证xx,搜索关键词验证xx
int query1[DICT_SIZE];//条件属性的辅助匹配向量
int query2[DICT_SIZE];//条件属性值的辅助匹配向量
int query3[DICT_SIZE];//搜索属性的辅助匹配向量,因为所用数据集仅包含一个属性值,所以不需要匹配属性值,故不需要query2、query4
int query4[DICT_SIZE];//搜索属性值的辅助匹配向量
int d_label[FILE_SIZE]={0};
string condition_attribute;
string condition_attribute_value;
string search_attribute;
string search_attribute_field[FILE_SIZE];
int k1=0;//统计结果中条件关键词验证标签的个数
int k2=0;//统计结果中属性值和搜索关键词验证标签的个数
int i_d=0;//用于搜索函数中遍历所有doucment lebel
// struct of queue
Record queue[FILE_SIZE];
int front = 0, rear = -1, itemCount = 0;//插入第一个document之后变成2000,然后递减直至1
Record *insert(Record *data) {
if( itemCount != FILE_SIZE ) {
if( rear == FILE_SIZE-1 )
rear = -1;
queue[++rear] = *data;
itemCount++;
}
return &queue[rear];
}
// read tree from file,注意此部分读入顺序要与生成索引树的参数顺序一样
Record *file2record( FILE *fp ) {
if( feof(fp) != 0 ) return NULL;
int i, p;
char att1[50];
char val1[50];
char att2[50];
char val2[50];
char att3[50];
char val3[50];
char att4[50];
char val4[50];
char att5[50];
char val5[50];
char att6[50];
char val6[50];
char att7[50];
char val7[50];
char att8[50];
char val8[50];
Record re;
// lid
fscanf(fp, "%d ", &re.lid);
// att1
fscanf(fp, "%s ", att1);
re.att1=att1;
// B_att1
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att1[i]);
}
// val1
fscanf(fp, "%s ", val1);
re.val1=val1;
// B_val1
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val1[i]);
}
// att2
fscanf(fp, "%s ", att2);
re.att2=att2;
// B_att2
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att2[i]);
}
// val2
fscanf(fp, "%s ", val2);
re.val2=val2;
// B_val2
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val2[i]);
}
// att3
fscanf(fp, "%s ", att3);
re.att3=att3;
// B_att3
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att3[i]);
}
// val3
fscanf(fp, "%s ", val3);
re.val3=val3;
// B_val3
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val3[i]);
}
// att4
fscanf(fp, "%s ", att4);
re.att4=att4;
// B_att4
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att4[i]);
}
// val4
fscanf(fp, "%s ", val4);
re.val4=val4;
// B_val4
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val4[i]);
}
// att5
fscanf(fp, "%s ", att5);
re.att5=att5;
// B_att5
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att5[i]);
}
// val5
fscanf(fp, "%s ", val5);
re.val5=val5;
// B_val5
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val5[i]);
}
// att6
fscanf(fp, "%s ", att6);
re.att6=att6;
// B_att6
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att6[i]);
}
// val6
fscanf(fp, "%s ", val6);
re.val6=val6;
// B_val6
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val6[i]);
}
// att7
fscanf(fp, "%s ", att7);
re.att7=att7;
// B_att7
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att7[i]);
}
// val7
fscanf(fp, "%s ", val7);
re.val7=val7;
// B_val7
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val7[i]);
}
// att8
fscanf(fp, "%s ", att8);
re.att8=att8;
// B_att8
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_att8[i]);
}
// val8
fscanf(fp, "%s ", val8);
re.val8=val8;
// B_val8
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%d ", &re.B_val8[i]);
}
// pointer
fscanf(fp, "%d ", &p);
printf("read the file%d\n", re.lid);
// recursive call
if( p != -1 )
re.P = file2record( fp );
else
re.P = NULL;
if( (re.P != NULL && p != re.P->lid)) {
printf("Error at reading Record %d from file\n", re.lid);
}
return insert(&re);
}
// search function
void search(Record *record, string search_attribute_field[FILE_SIZE], int thr1, int thr3) {
int j;
int result = 0;//存放每个节点与查询向量的内积值
if (d_label[i_d]=0)
{
puts("No more result");//搜索结果输出完毕
return ;
}
if (record->lid=d_label[i_d])//如果索引树的搜索结果里有该文件标签,则在该文件里找到想要的验证标签和属性字段
{
//再找到搜索属性的属性字段和他们的atag‘
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att1[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val1;
k2++;
}
else
{
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att2[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val2;
k2++;
}
else
{
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att3[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val3;
k2++;
}
else
{
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att4[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val4;
k2++;
}
else
{
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att5[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val5;
k2++;
}
else
{
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att6[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val6;
k2++;
}
else
{
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att7[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val7;
k2++;
}
else
{
for (j = 0; j < DICT_SIZE; j++)
{
result += query3[j] * record->B_att8[j];//计算每个节点与查询向量的内积值
}
if (result >= thr3 )
{
search_attribute_field[k2]=record->val8;
k2++;
}
}
}
}
}
}
}
}
i_d++;
search(record->P, search_attribute_field, thr1, thr3);
}
else
{
search(record->P, search_attribute_field, thr1, thr3);
}
}
int main( void ) {
int i, j;
int thr1=11, thr2, thr3=10, thr4;//根据需要手动更改阈值,需要自己估计,因为所用数据集仅包含一个属性值,所以不需要匹配属性值,故不需要thr3、thr4,
FILE *fp;
Record *record;
struct timeval starttime, endtime;
double t = 0; //计算时间
gettimeofday(&starttime, NULL);
// read tree from file
fp = fopen("./output/edocumentcollectionread2000.txt", "r");
if (!fp)
{
printf("Cannot open the file");
exit(1);
}
if(fp)
{
printf("Open the file\n");
}
record = file2record(fp);
fclose(fp);
puts("\n\n*** Finish reading the encrypted document collection from file. ***\n\n");
// do search
memset(query1, 0, sizeof(query1));
memset(query2, 0, sizeof(query2));
memset(search_attribute_field, 0, sizeof(search_attribute_field));
memset(d_label, 0, sizeof(d_label));
// read the matching vector from file
// 以workclassstategov为条件关键词,搜索属性为female的属性字段
fp = fopen("./source/attribute_auxiliary_matching_information/attrmatch1.txt", "r");//attrmatch1.txt最大阈值为11
if (!fp)
{
printf("Cannot open the file");
exit(1);
}
if(fp)
{
printf("Open the matching vector file\n");
}
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%1d ", &query1[i]);
printf("%d ", query1[i]);//用于查看查询向量
}
fclose(fp);
puts("\n\n*** Finish reading the matching vector from file. ***\n\n");
// read the matching vector from file
fp = fopen("./source/attribute_value_auxiliary_matching_information/attrmatch7.txt", "r");//attrmatch7.txt最大阈值为10
if (!fp)
{
printf("Cannot open the file");
exit(1);
}
if(fp)
{
printf("Open the matching vector file\n");
}
for( i = 0; i < DICT_SIZE; i++ ) {
fscanf(fp, "%1d ", &query3[i]);
printf("%d ", query3[i]);//用于查看查询向量
}
fclose(fp);
puts("\n\n*** Finish reading the matching vector from file. ***\n\n");
//puts("threshold:");//手动输入阈值,需要自己估计
//scanf("%lf", &thr);
// really do search
search(record, search_attribute_field, thr1, thr3);
//输出搜索结果,理想结果样式:文件xxx中,符合条件的属性字段为xxx,条件关键词验证xx,搜索关键词验证xx
puts("\n\nReaults = ");
for( i = 0; i < FILE_SIZE; i++ ) {
if( d_label[i] > 0 )//如果结果列表中有合法的文件标签,才输出,不合法的文件标签都为0
printf("\t%d document lable = %d query_attribute_field = %s \n",i+1, d_label[i], search_attribute_field[i].c_str());//输出结果序号、文件标签、内积值
else {
puts("No more result");//搜索结果输出完毕
break;
}
}
gettimeofday(&endtime, NULL);
t = 1000 * (endtime.tv_sec - starttime.tv_sec) + (endtime.tv_usec - starttime.tv_usec)/1000; //ms
printf("the times of search is:%lf",t);
printf("ms\n");
return 0;
}