将链接列表保存在结构中 [英] Save link list in structure

查看:86
本文介绍了将链接列表保存在结构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个链接列表(关于学生的信息)作为我的作业,我需要将链接列表保存到.txt文件中.我创建了一个保存函数,如下所示:

I have a link list(about student''s information) as my assignment and I am required to save the link list into a .txt file. I created a save function as below:

void output_list()
{
    STUDENT *p2;
    p2=header;
    printf("Enter the file name that you want to save: ");
    scanf("%s",filename);
    fptr= fopen(filename, "w");
    if (fptr == NULL){
        printf("Error, can't open the input file %s \n", filename);
    }
    else {
        while(p2 != NULL){
            fprintf(fptr,"%s %s %ld %d %ld \n", p2->firstname, p2->lastname, p2->idnumber, p2->ccode, p2->phonenum);
            p2 = p2->next;
            printf("%i",counter++);
            }
    }
    printf("File saved to %s \n",filename);
    fclose(fptr);
}



保存后,我在列表中有四个学生的情况下结束了该程序.当我用C打开文件时,它将5个学生加载到程序中.请帮助我解决这种情况.谢谢.

我的文件打开代码:



After saving it, I end the program with four students in the list. When I open the file in C, it loads 5 students into the program. Please help me to fix this situation. Thank You.

My file open code:

STUDENT *p, *q;
q = NULL;
printf(" Enter the input file name: ");
gets(filename);
fptr= fopen(filename, "r");
if (fptr == NULL)
    printf("Error, can't open the input file %s \n", filename);
else
{
    while (!feof(fptr))
    {
        fscanf(fptr," %s %s %ld %d %ld", fname, lname, &id, &course, &phone);
        /* printf("%s  %s  %ld %d %ld \n",fname, lname,id,course,phone);*/
        p=makenode(id,course, phone, fname, lname);
        p->next= q;
        q=p;
        printf("%i",counter++);
    }

推荐答案

有人可以帮我检查我的代码是否正确.紧急情况.谢谢."

对你来说很紧急.
甚至在我今天要做的前100件事中都没有.
您是否知道仅仅说我现在需要它"是多么粗鲁的举动?
您是否意识到这是一个周末,并且大多数人都在尽力娱乐?
你知道你今天不需要做作业吗?你的导师也不工作...

但是,也许您可​​以在一些帮助下自己解决此问题.

帮助我检查我的代码是否正确"
好吧,您已经做到了这一点:它是否可以生成您想要的结果?不,所以这是不正确的.您的请求已完成.


为什么它不产生您期望的结果?现在,这是一个更好的问题.

我看到您尝试通过添加跟踪语句来解决这个问题.您得到了什么结果?
它打印了什么?如果您不记得了,请取消注释,然后重试.这次,使其看起来像这样:
"Can anyone please help me check whether my code is correct. It''s urgent. Thank You."

It''s urgent to you.
It isn''t even in my top 100 things-to-do-today.
Do you realize how rude it is to bump your post, just to say "I need it now"?
Do you realise it''s a weekend, and most people are trying to enjoy themselves?
Do you realize your homework doesn''t even have to be in today - you tutor isn''t working either...

However, perhaps you can solve this yourself, with a bit of help.

"help me check whether my code is correct"
Well, you have done that bit: Is it generating the results you want? No. So, it isn''t correct. Your request is complete.


Why isn''t it generating the results you expect? Now that''s a better question.

I see you have made a stab at trying to work it out by putting a tracing statement in. What results did you get?
What did it print? If you can''t remember, then uncomment it and try again. This time, make it look like this:
printf("LOAD (%d): %s  %s  %ld %d %ld \n",counter, fname, lname,id,course,phone);


您的文件打开代码应类似于:

Your file open code should look like:

while (!feof(fptr))
{
    fscanf(fptr," %s %s %ld %d %ld", fname, lname, &id, &course, &phone);
    /* printf("%s  %s  %ld %d %ld \n",fname, lname,id,course,phone);*/
    p=makenode(id,course, phone, fname, lname);
    if (q)
        q->next = p;
    q=p;
    printf("%i",counter++);
}


保存文件时,将5个变量打印到文件中,后跟换行符.
但是,在加载文件时,您只能读取5个变量,而不能读取换行符.

这意味着当您读完最后一个结构时,仍然需要读取换行符,因此文件结尾检查不正确.空格带来与换行符相同的问题.
要解决此问题,您可以像这样保存:

When saving the file, you print the 5 variables to file, followed by a newline character.
When loading the file though, you only read the 5 variables and not the newline character.

This means that when you have read the last structure, there is still a newline character left to be read, so the end-of-file check isn''t true. Spaces give the same problem as newline characters.
To fix this, you could save like this:

fprintf(fptr," %s %s %ld %d %ld", p2->firstname, p2->lastname, p2->idnumber, p2->ccode, p2->phonenum);



如果您希望保留当前的文件格式(例如,因为对于人类来说更容易阅读),则可以使用fgets加载文件,然后使用sscanf而不是fscanf处理输入.然后,您将检查fgets返回的字符串的字符串长度,而不是检查文件结尾.



If you wish to keep the current file format (for example because it''s easier to read for humans), you could load your file using fgets and then processing the input with sscanf instead of fscanf. Then instead of checking for end-of-file, you would check for the string length of the string returned by fgets.


这篇关于将链接列表保存在结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆