我有以下功能:
void find(void) {
nd = (struct node*)malloc(sizeof(struct node) * 14);
FILE *openedfile;
openedfile = fopen("NSFNet.txt", "r");
if (openedfile == NULL) {
printf("Error, file no existente");
exit(1);
}
fseek(openedfile, 175, SEEK_SET);
char linea[300];
char *aux;
fgets(linea, 300, openedfile);
char *checker ="0";
int counter = 0;
int i = 0;
while (fgets(linea, 300,openedfile) != NULL) {
aux = strtok(linea, "[]");
int value = atoi(aux);
if (value == i) {
counter++;
}
if (value != i) {
nd[i].links = counter;
printf("El contador es:n");
printf("%dn", i);
printf("%dn", nd[i].links);
counter = 1;
i++;
}
}
}
下一个:
void test(void) {
nd = (struct node*)malloc(sizeof(struct node) * 14);
FILE *openedfile;
openedfile = fopen("NSFNet.txt", "r");
if (openedfile == NULL) {
printf("Error, file no existente");
exit(1);
}
fseek(openedfile, 175, SEEK_SET);
char linea[300];
char *aux;
fgets(linea, 300, openedfile);
char *checker ="0";
int counter = 0;
int i = 0;
while (fgets(linea, 300,openedfile) != NULL) {
aux = strtok(linea, "]");
aux = strtok(NULL, "]");
aux = strtok(NULL, "t");
printf("%s n",aux);
}
}
该代码从以下txt文件中提取一些数据:
Number of nodes: 14
Number of links: 42
==================================================
source dest. hops path (link ids)
==================================================
[0] [1] 1 0
[0] [2] 1 2
[0] [3] 2 0-8
[0] [4] 3 0-8-12
[0] [5] 2 2-10
[0] [6] 4 0-8-12-18
[0] [7] 1 4
[0] [8] 2 4-26
[0] [9] 3 4-26-28
[0] [10] 3 0-8-14
第一代码提取第一列的数字,第二代码提取第三列的数字。两个函数都可以单独工作,但我想创建一个条件。我把两个函数混合成一个函数:
void find2(void) {
nd = (struct node*)malloc(sizeof(struct node) * 14);
FILE *openedfile;
openedfile = fopen("NSFNet.txt", "r");
if (openedfile == NULL) {
printf("Error, file no existente");
exit(1);
}
fseek(openedfile, 175, SEEK_SET);
char linea[300];
char *aux;
char *aux2;
fgets(linea, 300, openedfile);
char *checker ="0";
int counter = 0;
int i = 0;
while (fgets(linea, 300,openedfile) != NULL) {
aux2 = strtok(linea, "]");
aux2 = strtok(NULL, "]");
aux2 = strtok(NULL, "t");
int value = atoi(aux);
int hops = atoi(aux2);
if (value == i && hops == 1) {
counter++;
}
if (value != i) {
nd[i].links = counter;
printf("El contador es:n");
printf("%dn", i);
printf("%dn", nd[i].links);
counter = 1;
i++;
}
}
}
混合函数有以下错误:exited, segmentation fault.
求你了,有人能帮我找出我的错误吗。
它是一个空值。我正在将字符串(null(转换为整数,这就是出现错误的原因。
谢谢大家!