| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1764 人关注过本帖
标题:链表到文件和文件到链表问题
只看楼主 加入收藏
新月bo
Rank: 2
等 级:论坛游民
帖 子:11
专家分:10
注 册:2020-3-13
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:7 
链表到文件和文件到链表问题
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

char ch = -1;
char change = -1;

typedef struct User
{
    char ID[20];
    char PW[20];
    char phonum[15];
    char address[100];
    char name[20];
    struct User *next;
} unode,*Ulist;

int listtofile(Ulist L,FILE *fp)
{
    Ulist p = L;
    while(p)
    {
        fprintf(fp,"%s %s %s %s %s\n",L->ID,L->PW,L->phonum,L->name,L->address);
        p = p->next;
    }
    fclose(fp);
    return 1;
}
/*
Ulist filetolist(FILE *fp)
{
    Ulist head = (Ulist)malloc(sizeof(unode));
    char ID[20];
    char PW[20];
    char phonum[15];
    char address[100];
    char name[20];
    Ulist p;
    Ulist q;
    p=q=head;
    if(fp==NULL)
    {
        printf("打开文件失败!");
        return NULL;
    }
    while(fscanf(fp,"%s %s %s %s %s",ID,PW,phonum,name,address)!=EOF)
    {
        q=(Ulist)malloc(sizeof(unode));
        strcpy(q->ID,ID);
        strcpy(q->PW,PW);
        strcpy(q->phonum,phonum);
        strcpy(q->address,address);
        strcpy(q->name,name);
        p->next=q;
        p=q;
    }
    p->next=NULL;
    return head;
}
*/

Ulist filetolist(FILE *fp)
{
    Ulist head = (Ulist)malloc(sizeof(unode));
    char ID[20];
    char PW[20];
    char phonum[15];
    char address[100];
    char name[20];
    Ulist p;
    Ulist q;
    p=q=head;
    if(fp==NULL)
    {
        printf("打开文件失败!");
        return NULL;
    }
    freopen("admin.txt","r",stdin);
    while(scanf("%s%s%s%s%s",ID,PW,phonum,name,address)!=EOF)
    {
        q=(Ulist)malloc(sizeof(unode));
        strcpy(q->ID,ID);
        strcpy(q->PW,PW);
        strcpy(q->phonum,phonum);
        strcpy(q->address,address);
        strcpy(q->name,name);
        p->next=q;
        p=q;
    }
    p->next=NULL;
    freopen("CON","r",stdin);
    return head;
}

int checkformat(char s[])//这里可以修改特定要求格式(我只要求大于0就可)
{
    if (strlen(s) == 0)
    {
        printf("长度应大于0!");
        return 0;
    }
    else
        return 1;
}

Ulist Writein(Ulist &L,char ID[],char PW[])
{
    Ulist p = (Ulist)malloc(sizeof(unode));
    strcpy(p->ID,ID);
    strcpy(p->PW,PW);
    p->next = L;
    L = p;
    printf("%s",L->ID);
    return L;
}

int FindUlist(Ulist L, char ID[])
{
    Ulist p = L;
    while(p)
    {
        if(strcmp(p->ID, ID) == 0)
        {
            return 1;
        }
        p = p->next;
    }
    return 0;
}

Ulist Search(Ulist L, char ID[], char PW[])
{
    Ulist p = L;
    while(p)
    {
        if(strcmp(p->ID, ID) == 0 && strcmp(p->PW, PW))
        {
            return L;
        }
        p = p->next;
    }
    return NULL;
}

int Register(Ulist ahead, Ulist chead, Ulist &admin, Ulist &client)
{
    while(1)
    {
        char ID[20],PW[20],CF[20];//账号,密码,确认密码
        char define;
        printf("请输入你要注册的用户类型:\n");
        printf("【1】客户\n");
        printf("【2】管理员\n");
        printf("【0】返回上一级菜单\n");
        define = getch();
        switch(define)
        {
            case '0':
                system("cls");
                return 0;break;
            case '1':
            case '2':
                for (int i = 0; i < 3; i++)
                {
                    printf("请输入要注册的用户名:");
                    scanf("%s",ID);
                    if(!checkformat(ID))//检查格式
                        break;
                    printf("\n");
                    printf("请输入密码:");
                    scanf("%s",PW);
                    if(!checkformat(PW))
                        break;
                    printf("\n");
                    printf("请再次确认密码:");
                    scanf("%s",CF);
                    if (strcmp(PW,CF) != 0)//检查是否一致
                    {
                        printf("两次密码输入不一致!\n");
                        Sleep(500);
                        system("cls");
                        continue;
                    }
                    if (define == '1')
                    {
                        if(FindUlist(chead,ID))
                            printf("该用户名已存在!\n");
                        else
                        {
                            client = Writein(chead,ID,PW);
                            printf("注册成功!\n");
                            Sleep(500);
                            system("cls");
                            return 1;
                        }
                    }
                    if (define == '2')
                    {
                        if(FindUlist(ahead,ID))
                            printf("该用户名已存在!\n");
                        else
                        {
                            client = Writein(ahead,ID,PW);
                            printf("注册成功!\n");
                            Sleep(500);
                            system("cls");
                            return 2;
                        }
                    }
                }
                break;
            default:
                printf("非法输入!请重新输入!");
                Sleep(500);
                system("cls");
        }
    }
}


int Login(Ulist ahead,Ulist chead,Ulist admin,Ulist client)
{
    char define;//区分客户(标记1)和管理员(标记2)
    char ID[20],PW[20];//账号和密码
    while(1)
    {
        printf("【1】客户登录\n");
        printf("【2】管理员登录\n");
        printf("【0】返回菜单\n");
        define = getch();
        Ulist permit = NULL;
        switch(define)
        {
            case '0':
                system("cls");
                return 0;
                break;
            case '1':
            case '2'://由define决定登录类型
                for (int i = 0; i < 3; i++)
                {
                    printf("请输入用户名:");
                    scanf("%s",ID);
                    printf("请输入密码:");
                    scanf("%s",PW);
                    if (define == '1')
                        permit = Search(chead,ID,PW);
                    else if (define == '2')
                        permit = Search(ahead,ID,PW);
                    system("cls");
                    if(permit)
                        break;
                    else
                        printf("您输入的用户名或密码错误!");
                }
                break;
            default:
                printf("非法输入,请重新选择!");
                Sleep(500);
                system("cls");
                break;
        }
        //判断允许登录还是返回上级菜单
        if (permit)
        {
            printf("欢迎回来!");
            return define - '0';//由define判断登录类型
        }
        else
        {
            printf("你输入的次数过多,请稍后重试!");
            return 0;
        }
    }
}

void welcomenu(Ulist ahead, Ulist chead,Ulist &admin, Ulist client)
{
    FILE *fa = fopen("admin.txt","w");
    FILE *fc = fopen("client.txt","w");
    while (change != 1 && change != 2)
    {   printf("欢迎使用菜鸟驿站智能管理系统!\n");
        printf("【1】用户登录\n");
        printf("【2】用户注册\n");
        printf("【3】退出系统\n");
        ch = getch();
        switch(ch)
        {
            case '1':
                system("cls");
                change = Login(ahead,chead,admin,client); break;
            case '2':
                system("cls");
                change = Register(ahead,chead,admin,client); break;
            case '3':
                system("cls");
                printf("欢迎下次使用!");
                listtofile(chead,fa);
                listtofile(ahead,fc);
                exit(0);
            default:
                printf("非法输入!请重新输入!");
                Sleep(500);
                system("cls");
        }
    }
}

int adminmenu(Ulist &admin)
{
    do
    {
        printf("管理员菜单:\n");
        printf("【1】到达包裹录入\n");
        printf("【2】到达包裹转寄\n");
        printf("【3】到达包裹统计\n");
        printf("【4】待寄包裹录入\n");
        printf("【5】待寄包裹删除\n");
        printf("【6】待寄包裹分类\n");
        printf("【0】退出登录");
        ch = getch();
        switch(ch)
        {
            case '1':
                system("cls");
                break;
            case '2':
                system("cls");
                 break;
            case '3':
                system("cls");
                break;
            case '4':
                system("cls");
                break;
            case '5':
                system("cls");
                break;
            case '6':
                system("cls");
                break;
            case '0':
                system("cls");
                return 0;
            default:
                printf("非法输入!请重新操作。");
                Sleep(500);
                system("cls");
        }
        system("cls");
    } while(1);
}

int clientmenu(Ulist client)
{
    do
    {
        printf("客户菜单:\n");
        printf("【1】取快递\n");
        printf("【2】寄快递\n");
        printf("【3】修改资料\n");
        printf("【4】反馈建议\n");
        printf("【0】退出登录\n");
        ch = getch();
        switch(ch)
        {
            case '1':
                system("cls");
                break;
            case '2':
                system("cls");
               break;
            case '3':
                system("cls");
                break;
            case '4':
                system("cls");
                break;
            case '0':
                system("cls");
                return 0;
                break;
            default:
            printf("非法输入!请重新操作。");
            Sleep(500);
            system("cls");
        }
    }while (1);
}


int main(void)
{
    FILE *fa,*fc;
    fa = fopen("admin.txt","r");
    fc = fopen("client.txt","r");
    Ulist client = NULL;//指向登录后用户节点
    Ulist chead = filetolist(fc);//头结点
    Ulist admin = NULL;//指向登陆后用户结点
    Ulist ahead = filetolist(fa);//头结点
    fclose(fc);
    fclose(fa);
    do
    {
        ch = -1;
        welcomenu(ahead,chead, admin,client);
        switch(change)
        {
            case 1:change = clientmenu(client); break;
            case 2:change = adminmenu(admin);break;
        }
    } while(change == 0);
    return 0;
}

大牛们,我查了好久资料,迫不得已发帖提问,我实在不知道为什么我编写的不能实现将用户传入文件或将文件传入用户,问题主要在listtofile和filetolist函数上,我很烦恼自己对文件和链表的掌握不熟悉,希望各位大牛能指点指点我的课设,谢谢了(有一些被我乱改了,不要介意)

搜索更多相关主题的帖子: printf char system break case 
2020-04-01 17:07
新月bo
Rank: 2
等 级:论坛游民
帖 子:11
专家分:10
注 册:2020-3-13
收藏
得分:0 
为啥我贴了代码就不能在代码块外打字了
2020-04-01 17:08
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10538
专家分:42927
注 册:2014-5-20
收藏
得分:5 
只是结构数据的文件读写问题可以精简点代码来测试
2020-04-01 17:12
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:5 
文件内容呢

https://zh.
2020-04-01 17:15
新月bo
Rank: 2
等 级:论坛游民
帖 子:11
专家分:10
注 册:2020-3-13
收藏
得分:0 
回复 4楼 lin5161678
请问您的意思是。。
2020-04-01 17:25
return_0
Rank: 8Rank: 8
来 自:五维空间
等 级:禁止访问
威 望:3
帖 子:512
专家分:838
注 册:2020-1-28
收藏
得分:0 
以下是引用scott520在2020-4-2 08:49:21的发言:

60/年,香港网站空间,高速稳定,延迟15ms  
https://www.

这算是广告吧,大家管一下啊

2020-04-02 09:07
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:10 
指出几处明显的错误:
1,listtofile函数中,移动指针p,往文件写的是指针L指向的数据。
2,filetolist函数中,使用的head没看见你定义的全局变量,是野指针。参数fp传入后唯一的作用是判断是否有效,无效退出,有效没发挥作用,数据都是从admin文件中读出来的。

能编个毛线衣吗?
2020-04-02 10:04
新月bo
Rank: 2
等 级:论坛游民
帖 子:11
专家分:10
注 册:2020-3-13
收藏
得分:0 
回复 8楼 wmf2014
谢谢谢谢!我已经解决了,可能是一天都在电脑前赶课设,眼镜都不灵光了。还是谢谢你!
2020-04-02 10:11
快速回复:链表到文件和文件到链表问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039020 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved