昨天我打开了一个帖子询问我应该怎么做。这是我到目前为止所做的事情(对不起语言,该程序将采用阿尔巴尼亚语,但我相信你明白了这一点。)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void regjistrim();
void kerkim();
void modifikim();
void fshirje();
void rradhitje();
void display();
void load();
#define EMRI 50
#define MBIEMRI 50
#define ID 20
#define TEL 20
#define EMAIL 25
typedef struct node
{
char emri[EMRI];
char mbiemri[MBIEMRI];
char id[ID];
char tel[TEL];
char email[EMAIL];
struct node* next;
} node;
FILE* addressbook;
node* mynode;
node* curr;
int main(void)
{
char input[2];
int choice;
load();
printf("----------------ADDRESS BOOK----------------");
printf("\n\n\t1 - Regjistrimi i ri\n");
printf("\n\t2 - Kerkim\n");
printf("\n\t3 - Modifikim\n");
printf("\n\t4 - Fshirje\n");
printf("\n\t5 - Rradhitje\n");
printf("\n\t6 - Afishim i address book\n");
printf("\n\t0 - Exit\n");
fgets(input, 4, stdin);
sscanf(input, "%d", &choice);
while (choice < 0 || choice > 6)
{
printf("\nShtypni nje numer nga 0 - 6: \n");
fgets(input, 4, stdin);
sscanf(input, "%d", &choice);
}
switch (choice)
{
case 1:
regjistrim();
break;
case 2:
kerkim();
break;
case 3:
modifikim();
break;
case 4:
fshirje();
break;
case 5:
rradhitje();
break;
case 6:
display();
break;
case 0:
exit(0);
break;
}
return 0;
}
void load()
{
addressbook = fopen("Addressbook.txt", "r");
if (addressbook == NULL)
printf("Could not open file.");
node* mynode = malloc(sizeof(node));
while (fscanf(addressbook, "%s %s %s %s %s", mynode->emri, mynode->mbiemri, mynode->id, mynode->tel, mynode->email) != EOF)
{
node* mynode = malloc(sizeof(node));
mynode = mynode->next;
}
mynode = curr;
}
//Regjistron nje qytetar ne addressbook
void regjistrim()
{
char input[2];
char answer;
do
{
node* newnode = malloc(sizeof(node));
curr->next = newnode;
addressbook = fopen("Addressbook.txt", "a+");
printf("\nShtypni emrin: ");
fgets(newnode->emri, EMRI, stdin);
printf("\nShtypni mbiemrin: ");
fgets(newnode->mbiemri, MBIEMRI, stdin);
printf("\nShtypni ID-in: ");
fgets(newnode->id, ID, stdin);
printf("\nShtypni nr. telefoni: ");
fgets(newnode->tel, TEL, stdin);
printf("\nShtypni email-in: ");
fgets(newnode->email, EMAIL, stdin);
fprintf(addressbook, "Emri: %sMbiemri: %sID: %sNr. telefoni: %sEmail: %s\n", newnode->emri, newnode->mbiemri, newnode->id, newnode->tel, newnode->email);
fclose(addressbook);
printf("\nShtypni y/Y neqoftese doni te regjistroni person tjeter: ");
fgets(input, 50, stdin);
sscanf(input, "%c", &answer);
curr = newnode;
}
while(answer == 'y' || answer == 'Y');
}
void kerkim()
{
//TODO
}
void modifikim()
{
//TODO
}
void fshirje()
{
//TODO
}
void rradhitje()
{
//TODO
}
void display()
{
//TODO
}
目前我正在收到一个段错误:while (fscanf(addressbook, "%s %s %s %s %s", mynode->emri, mynode->mbiemri, mynode->id, mynode->tel, mynode->email) != EOF)
我主要关注的是load()
函数,它会在用户执行任何操作之前将任何条目加载到链表中(如果有的话)。
提前谢谢。
答案 0 :(得分:0)
我建议您查看链接列表的现有实现。添加辅助函数以插入节点,附加节点,删除节点以及那些类型的东西。一旦您有了列表本身的工作实现,您将更容易编写利用链表数据结构的代码。
考虑更改node
结构以包含指向实际数据的指针和指向下一个节点的指针(而不是实际拥有struct
本身中的数据)。这样做更好。您甚至可以使用void指针作为“通用”指针来扩展链表实现以支持任何数据类型。
链接列表的分段错误通常表示您在代码中的某处取消引用了NULL指针(但并非总是如此!)。确保您没有引用已删除的节点或尚未分配的节点。
如果您对当前的实现感到满意,请使用gdb
逐步执行代码,并设置程序为segfaulting的断点。你不应该很难找到错误的位置。
答案 1 :(得分:0)
如果要创建链接列表,请尝试执行此操作 替换此代码
node* mynode = malloc(sizeof(node));
while (fscanf(addressbook, "%s %s %s %s %s", mynode->emri, mynode->mbiemri, mynode->id, mynode->tel, mynode->email) == 5)
{
node* mynode = malloc(sizeof(node));
mynode = mynode->next;
}
mynode = curr;
用这个
node* mynodeLocal = malloc(sizeof(node));
node *secondLastNode;
while (fscanf(addressbook, "%s %s %s %s %s", mynodeLocal->emri, mynodeLocal->mbiemri, mynodeLocal->id, mynodeLocal->tel, mynodeLocal->email) != EOF)
{
secondLastNode = mynodeLocal;
mynodeLocal->next = malloc(sizeof(node));
mynodeLocal = mynodeLocal->next;
mynodeLocal->next = NULL;
}
secondeLastNode->next = NULL;
mynode = curr;//Change this line according to your requirement.