【发布时间】:2018-02-14 09:46:24
【问题描述】:
我有一个 txt 文件,我只想获取大于 12 个字符的行。然后将这些线插入到名为 graph_node 类型的 graph 的变量中。
txt 文件:
1,"execCode(workStation,root)","OR",0
2,"RULE 4 (Trojan horse installation)","AND",0
3,"accessFile(workStation,write,'/usr/local/share')","OR",0
4,"RULE 16 (NFS semantics)","AND",0
5,"accessFile(fileServer,write,'/export')","OR",0
6,"RULE 10 (execCode implies file access)","AND",0
7,"canAccessFile(fileServer,root,write,'/export')","LEAF",1
6,7,-1
图形节点类型:
#ifndef Graph_Structure
#define Graph_Structure
struct Graph_Node{
char id[50];
char node_name[50];
struct Graph_Node* next;
struct Graph_Node* edges;
};
typedef struct Graph_Node graph_node;
#endif
这是将数据插入图形变量的方法:
void insert_node(graph_node** node, graph_node* data){
printf("\nINSERTING\n");
graph_node* temp = (graph_node*)malloc(sizeof(graph_node));
for(int i = 0; i < strlen(data->id); i++)
temp->id[i] = data->id[i];
for(int i = 0; i < strlen(data->node_name) - 1; i++)
temp->node_name[i] = data->node_name[i];
temp -> next = *node;
*node = temp;
}
这是获取txt文件中大于12个字符的行的方法:
void generate_nodes(graph_node** graph, char* file_name){
graph_node* data = (graph_node*)malloc(sizeof(graph_node));
FILE* f_Data_Pointer = fopen(file_name, "r");
FILE* f_Aux_Pointer = fopen(file_name, "r");
char c = 0; char line[256];
int counter = 0;
int q = 0; //quotation marks
bool jump_line = false;
while(!feof(f_Data_Pointer)){
c = 0; memset(line, 0, sizeof(line));
while(c != '\n' && !feof(f_Aux_Pointer)){ // check line
c = fgetc(f_Aux_Pointer);
line[counter] = c;
counter++;
}
if(strlen(line) > 12){ //lines with no edges
/*line[counter-3] != '-' && line[counter-2] != '1'*/
int size = strlen(line); printf("\nline size: %d\n", size);
counter = 0; c = 0;
while(c != ','){ //id
c = fgetc(f_Data_Pointer);
if(c != ','){
data->id[counter] = c;
counter++;
}
printf("%c", c);
}
counter = 0; c = 0;
while(1){ //node_name
c = fgetc(f_Data_Pointer);
if(c != '"'){
data->node_name[counter] = c;
counter++;
}
else{
q++;
}
if(q > 1 && c == ',')
break;
printf("%c", c);
}
counter = 0; c = 0;
while(c != '\n'){
c = fgetc(f_Data_Pointer);
printf("%c", c);
}
insert_node(graph, data);
memset(data->id, 0, sizeof(data->id));
memset(data->node_name, 0, sizeof(data->node_name));
}
else{ //lines with edges
while(c != '\n' && !feof(f_Data_Pointer)){
c = fgetc(f_Data_Pointer);
}
}
}
fclose(f_Data_Pointer);
fclose(f_Aux_Pointer);
}
我在“strlen”中的“for”命令的 insert 方法中遇到错误,它说 data->id 和 data->node_name 未初始化,但我不明白为什么。我在数据上使用了 malloc:
graph_node* data = (graph_node*)malloc(sizeof(graph_node));
错误:
Conditional jump or move depends on uninitialised value(s) ==3612== at 0x4C30B18: strcpy (vg_replace_strmem.c:510) ==3612== by 0x4008B2: insert_node (mulval.c:44) ==3612== by 0x400C03: generate_nodes (mulval.c:159) ==3612== by 0x400CE8: main (mulval.c:187)
【问题讨论】:
-
请发布显示问题的Minimal, Complete, and Verifiable example。如果没有双行间距,它会更具可读性,但有合理的空白。
-
在
insert_node中,您正在复制所有字符,除了以 0 结尾的字符。使用strcpy:strcpy(tmp->id, data->id); -
你听说过
strcpy/strncpy吗? :) -
始终测试来自
fopen的返回值。还有char c;==>int c;总是阅读手册页。