在链接列表中编辑配置文件

时间:2014-02-23 07:43:20

标签: c visual-studio-2010

这是我编辑个人资料的代码。它应该做的是编辑用户之前已输入的记录,但它没有编辑任何内容,当我输入信息时,它会立即打印未找到的记录。谁能识别出这个问题?问题在于编辑,但我也提到了其余的代码。

struct employee {
    char code[6];
    char name[15];
    char nationality[15];
    char gender[8];
    struct employee *newPtr;     
} *start, *curr;

void append() {
    curr = start;
    if(start == NULL) {     
        start = curr = (struct employee*)malloc(sizeof(struct employee));
        employee_entry();
        curr->newPtr = NULL;
        printf("\n\nFirst Node Added!!!");
        return;         
    }
    while(curr->newPtr)
        curr = curr->newPtr;
    curr->newPtr = (struct employee *)malloc(sizeof(struct employee));
    curr = curr->newPtr;
    employee_entry();
    curr->newPtr = NULL;
    printf("A new Node has been added to the list!!!");
}

void edit() {
    char name[8];    
    curr = start;    
    printf("\nEnter name to modify:");  
    gets_s(name);     
    while(curr) {
        if(strcmp(curr->name, name) == 0) {
            employee_entry();
            printf("\n\n\t\t The required data has been edited!!!");
            return;
        }
       curr = curr->newPtr;  // current pointer points to the next file 
       fprintf(stderr, "\n\n\t\tSorry record not found!!");
    }
}

1 个答案:

答案 0 :(得分:0)

当您在迭代列表时在循环内打印Sorry record not found!!时,您的代码会误导您。只需在循环外部获取错误消息,因为您只能得出在检查完所有列表条目后未找到的条目

while(curr) {
    if(strcmp(curr->name, name) == 0) {
        employee_entry();
        printf("\n\n\t\t The required data has been edited!!!");
        return;
    }
   curr = curr->newPtr;  // current pointer points to the next file 
   /// don't print here error message
}
//print here
fprintf(stderr, "\n\n\t\tSorry record not found!!");