【发布时间】:2017-11-01 03:12:45
【问题描述】:
read_to_list 函数旨在扫描文件的内容 - dump.txt - 并将其加载到单链表数组中。
问题出在main函数的LINE 2:read_to_list(argv[1]);
dump.txt 文件包含以下信息:
Name Group Size
----------------------------------
Anna 2
Bill 4
Connor 6
Denise 8
argv[1] 与我在命令行上声明的一个文本文件相关,以便在运行程序后从我的链表中转储数据。退出程序后,.txt 文件以上述格式保存数据。
我不知道为什么在 CLI 中传递的文件上调用 read_to_list 函数时出现分段错误,因为在我看来该函数应该正确加载数据并且该函数是在 main 函数的正确位置调用。
感谢您为我提供的任何帮助!
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node
{
char name[20];
int size;
struct node *next;
}node;
node* head[4]={NULL,NULL,NULL,NULL};
node* tail[4]={NULL,NULL,NULL,NULL};
void read_to_list(char *filename)
{
FILE *fp;
int mined_partysize;
char mined_partyname[20];
char header = "Name\t\tGroup Size\n----------------------------------\n";
fp = fopen(filename, "r");
if (fp == NULL)
return;
fseek(fp, strlen(header), SEEK_SET);
printf("This is working1");
while(fscanf(fp, "%s%d", mined_partyname, &mined_partysize) == 2)
{
printf("This is working2");
if(mined_partysize == 0)
{
printf("\nThat is not a valid command. Party not added!\n");
}
if(mined_partysize >= 1 && mined_partysize <= 2)
{
add_party(0, mined_partyname, mined_partysize);
}
else if(mined_partysize >= 3 && mined_partysize <= 4)
{
add_party(1, mined_partyname, mined_partysize);
}
else if(mined_partysize >= 5 && mined_partysize <= 6)
{
add_party(2, mined_partyname, mined_partysize);
}
else if(mined_partysize >= 7)
{
add_party(3, mined_partyname, mined_partysize);
}
}
fclose(fp);
return;
}
//
// main function
//
int main(int argc, char *argv[])
{
int x;
read_to_list(argv[1]);
while (1)
{
fflush(stdin);
printf("\n\nEnter 1 to add a party\nEnter 2 to remove a party\nEnter 3 for the list of the party\nEnter 4 to change party size.\nEnter 5 to quit.\n\n");
// user interface
scanf("%d",&x);
char name[20];
int size;
switch(x)
{
case 1:
printf("\nParty Name: ");
scanf("%s", name);
printf("\nParty Size: ");
scanf("%d", &size);
if(size == 0)
{
printf("\nThat is not a valid command. Party not added!\n");
}
if(size >= 1 && size <= 2)
{
add_party(0, name, size);
}
else if(size >= 3 && size <= 4)
{
add_party(1, name, size);
}
else if(size >= 5 && size <= 6)
{
add_party(2, name, size);
}
else if(size >= 7)
{
add_party(3, name, size);
}
break;
case 2:
printf("\nSize of party to delete: ");
scanf("%i", &size);
delete_party(NULL, size);
break;
case 3:
list_parties();
break;
case 4:
change_partysize(name, size);
break;
case 5:
write_to_file(argv[1]);
exit(0);
break;
default:
continue;
}
}
}
//
//add function
//
void add_party(int h, char *name, int size)
{
//declare file pointer
FILE *fp;
//to be used by fscanf
int number;
//create a new node
int i=0;
int breaker = 0;
node *p;
node *new_item;
new_item = (node *)malloc(sizeof(node)); // allocate memory the size of the struct
strcpy(new_item->name,name);
new_item->size = size;
if(head[h] == NULL && tail[h] == NULL) // if an empty list, create a head and tail
{
head[h] = new_item;
tail[h] = head[h];
new_item->next = NULL;
return;
}
//traversal
for(i=0; i<4; i++)
{
p = head[i];
while(p != NULL)
{
//check that no repeating names. delete nodes that do have repeating names
if(strcmp(p->name,name) == 0)
{
printf("\nSorry, that name is already taken\n");
free(new_item);
return;
}
p = p->next; //go to the next node in the list
}
}
tail[h]->next = new_item;
new_item->next = NULL;
tail[h] = new_item;
}
【问题讨论】:
-
你调试的时候到哪里去了?
-
第 134 行:read_to_list(arg[1])
-
那么进入
read_to_list... -
@John3136 这是 gdb 输出:(gdb) run ./a.out dump.txt 启动程序:/DCNFS/users/student/cbrereto/COEN11/a.out ./a.out dump.txt 程序收到信号 SIGSEGV,分段错误。 0x00007ffff7b7d921 in __strlen_sse2_pminub () from /lib64/libc.so.6
-
你需要学习使用gdb..尝试bt命令获取堆栈跟踪。
标签: c arrays file-io linked-list singly-linked-list