| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1415 人关注过本帖
标题:一个小程序,添加一条记录写入文件后,不能显示出来
只看楼主 加入收藏
我是胖子
Rank: 1
等 级:新手上路
帖 子:20
专家分:4
注 册:2013-5-12
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:6 
一个小程序,添加一条记录写入文件后,不能显示出来
程序代码:
# include <stdio.h>
# include <windows.h>
# include <stdlib.h>
# include <string.h>
struct book
{
    int book_number;
    char book_name[30];
    float book_price;
    char book_class[20];
    int book_sign;
}school_book[30];
void InformationSearch();
void BackStage();
bool Login();
void Initialize();
void AddItem();
void ChangeItem();
void DeletItem();
void ScanItem();
void ChangePassword();
void save(FILE * fp, int i);
void main()//主函数登陆
{
    printf("--------请输入登陆状态-------\n");
    printf("1.游客登录\n");
    printf("2.管理员登陆\n");

    int i;
    scanf("%d", &i);
    switch(i)
    {
    case 1:
        {
            system("cls");
            Sleep(800);
            InformationSearch();break;
        }
    case 2:
        {
            system("cls");
            Sleep(800);
            BackStage();break;
        }
    }
}
void InformationSearch()//信息查询系统
{
    FILE * fp;
    int siz, j, count = 0, i = 0;
    char ch;
    if((fp=fopen("Data", "rb+"))==NULL)
    {
        printf("打开数据文件Data错误!\n");
        exit(0);
    }
    printf(">>>>>>>>>>>>>>欢迎使用图书信息查询系统<<<<<<<<<<<<<<<\n");
    printf("编号   名称   价格(元)  类别\n");
    for(i=0;i<30;i++)
    {
        fread(&school_book[i], sizeof(struct book), 1, fp);
        if(school_book[i].book_number==0)
        {
            break;
        }
        printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class);
        printf("\n");
    }
}
void BackStage()//后台系统
{
    if(Login())
    {
        return;
    }
    while(1)
    {
        printf("--------欢迎登陆图书后台管理系统--------\n");
        printf("1.初始化 所有信息\n");
        printf("2.添加 图书信息\n");
        printf("3.修改 图书信息\n");
        printf("4.删除 图书信息\n");
        printf("5.查看 图书信息\n");
        printf("6.管理员密码修改\n");

        int i;
        scanf("%d", &i);
        switch(i)
        {
        case 1:
            {
                system("cls");
                Sleep(800);
                Initialize();
                break;
            }
        case 2:
            {
                system("cls");
                Sleep(800);
                AddItem();
                break;
            }
        case 3:
            {
                system("cls");
                Sleep(800);
                ChangeItem();
                break;
            }
        case 4:
            {
                system("cls");
                Sleep(800);
                DeletItem();
                break;
            }
        case 5:
            {
                system("cls");
                Sleep(800);
                ScanItem();
                break;
            }
        case 6:
            {
                system("cls");
                Sleep(800);
                ChangePassword();
                break;
            }
        }
    }
}
bool Login()//打开保存密码的文件,并验证
{
    FILE * fp;
    int count;
    char filename[] = "password";
    char stringIn[10];
    char stringOut[10];

//    if((fp=fopen(filename, "rb+"))==NULL)
//    {
//        printf("这是您首次使用本系统\n默认密码为admin\n");
//        fp=fopen(filename, "wb+");
//        fprintf(fp, "admin");
//        fclose(fp);
//    }
    while ((fp = fopen(filename, "rb+")) == NULL)
     {
         printf("这是您首次使用本系统\n默认密码为admin\n");
         fp = fopen(filename, "wb+") ;
         fprintf(fp, "admin");
         fclose(fp);
     }
    fscanf(fp, "%s", stringOut);
    printf("请输入密码: ");
    scanf("%s", stringIn);
    count = strcmp(stringIn, stringOut);
    if(count==0)
        return false;
    else
        return true;
}
void Initialize()
{
    char ch;
    int i = 0;
    FILE * fp;
    fp = fopen("Data", "wb+");
    do
    {
        printf("=================================\n");
        printf("请输入第%d项图书信息\n", i+1);
        printf("图书编号: ");
        scanf("%d", &school_book[i].book_number);
        ch = getchar();
        printf("图书名称: ");
        scanf("%s", school_book[i].book_name);
        ch = getchar();
        printf("图书价格: ");
        scanf("%f", &school_book[i].book_price);
        ch = getchar();
        printf("图书类别: ");
        scanf("%s", school_book[i].book_class);
        ch = getchar();
        school_book[i].book_sign = 1;
        printf("=================================\n");
        ++i;
        printf("是否继续录入?(Y/N)");
        ch = getchar();
    }while(ch=='y'&&'Y');
    save(fp, i);
    fclose(fp);
}
void AddItem()
{
    char ch;
    int i, j = 0;
    FILE * fp;
    if((fp = fopen("Data", "rb+"))==NULL)
    {
        printf("cannot open file!\n");
        return;
    }

    for(i=0;i<30;i++)
    {
        fread(&school_book[i], sizeof(struct book), 1, fp);
        if(school_book[i].book_number==0)
        {
            break;
        }
    }
    fclose(fp);
    j = i;
    do
    {
        printf("=================================\n");
        printf("请输入第%d项图书信息\n", i+1);
        printf("图书编号: ");
        scanf("%d", &school_book[i].book_number);
        ch = getchar();
        printf("图书名称: ");
        scanf("%s", school_book[i].book_name);
        ch = getchar();
        printf("图书价格: ");
        scanf("%f", &school_book[i].book_price);
        ch = getchar();
        printf("图书类别: ");
        scanf("%s", school_book[i].book_class);
        ch = getchar();
        school_book[i].book_sign = 1;
        printf("=================================\n");
   
        printf("是否继续录入?(Y/N)");
        ch = getchar();
        ++i;
    }while(ch=='y'&&'Y');
    if((fp = fopen("Data", "ab+"))==NULL)
    {
        printf("cannot open file!\n");
        return;
    }
    for(;j<i;j++)
    {
        if(fwrite(&school_book[j], sizeof(struct book), 1, fp)!=1)
        {
            printf("Error!\n");
            break;
        }
    }

    fclose(fp);
}
void ChangeItem()
{
   
    FILE * fp;
    int siz, h, j, i = 0;
    char ch;
    if((fp=fopen("Data", "rb+"))==NULL)
    {
        printf("打开数据文件Data错误!\n");
        exit(0);
    }
    printf("编号   名称   价格(元)  类别\n");
    for(i=0;i<30;i++)
    {
        fread(&school_book[i], sizeof(struct book), 1, fp);
        if(school_book[i].book_number==0)
        {
            break;
        }
        printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class);
        printf("\n");
    }
    fclose(fp);
    printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    printf("请输入要修改的记录序号:  ");
    scanf("%d", &j);
    fp = fopen("Data", "wb+");

    for(h=0;h<=i;h++)
    {
        if(school_book[h].book_number==j)
        {
            printf("=================================\n");
            printf("请输入第%d项图书信息\n", j);
            printf("图书编号: ");
            scanf("%d", &school_book[h].book_number);
            ch = getchar();
            printf("图书名称: ");
            scanf("%s", school_book[h].book_name);
            ch = getchar();
            printf("图书价格: ");
            scanf("%f", &school_book[h].book_price);
            ch = getchar();
            printf("图书类别: ");
            scanf("%s", school_book[h].book_class);
            ch = getchar();
            school_book[h].book_sign = 1;
            printf("=================================\n");
        }
        if(fwrite(&school_book[h], sizeof(struct book), 1, fp)!=1)
        {
            printf("Error!");
            break;
        }
    }
    save(fp, i);
    fclose(fp);
}
void DeletItem()
{

    FILE * fp;
    int siz, h, j, i = 0;
    char ch;
    if((fp=fopen("Data", "rb+"))==NULL)
    {
        printf("打开数据文件Data错误!\n");
        exit(0);
    }
    printf("编号   名称   价格(元)  类别\n");
    for(i=0;i<30;i++)
    {
        fread(&school_book[i], sizeof(struct book), 1, fp);
        if(school_book[i].book_number==0)
        {
            break;
        }
        printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class);
        printf("\n");
    }
    fclose(fp);
    printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
    printf("请输入要删除的记录序号:  ");
    scanf("%d", &j);
    fp = fopen("Data", "wb+");

    for(h=0;h<=i;h++)
    {
        if(school_book[h].book_number==j)
        {
            continue;
        }
        if(fwrite(&school_book[h], sizeof(struct book), 1, fp)!=1)
        {
            printf("Error!");
            break;
        }
    }
    save(fp, i);
    fclose(fp);
}
void ScanItem()
{
    FILE * fp;
    int siz, j, count = 0, i = 0;
    char ch;
    if((fp=fopen("Data", "rb+"))==NULL)
    {
        printf("打开数据文件Data错误!\n");
        exit(0);
    }
    printf("编号   名称   价格(元)  类别\n");
    for(i=0;i<30;i++)
    {
        fread(&school_book[i], sizeof(struct book), 1, fp);
        if(school_book[i].book_number==0)
        {
            break;
        }
        printf("%2d%8s%9.2f%9s", school_book[i].book_number, school_book[i].book_name, school_book[i].book_price, school_book[i].book_class);
        printf("\n");
    }

}
void ChangePassword()
{
    FILE * fp;
    int count;
    char filename[] = "password";
    char ch[10];
    char stringIn[10];
    char stringOut[10];

    fp = fopen(filename, "rb+");

    fscanf(fp, "%s", stringOut);
    printf("请输入旧密码: ");
    scanf("%s", stringIn);
    count = strcmp(stringIn, stringOut);
    if(count==0)
    {
        printf("密码验证成功\n新密码: ");
        scanf("%s", ch);
        fp = fopen(filename, "wb+") ;
        fprintf(fp, ch);
        fclose(fp);
    }
    else
    {
        printf("密码验证失败\n");
        return;
    }
}
void save(FILE * fp, int i)
{
//    fprintf(fp, "%d%10s%6.2f%10s", book[i].book_number, book[i].book_name, book[i].book_price, book[i].book_class);
//    fprintf(fp, "\n");
    int j;
    for(j=0;j<=i;j++)
    {
        if(fwrite(&school_book[j], sizeof(struct book), 1, fp)!=1)
        {
            printf("error!");
            break;
        }
    }
}
搜索更多相关主题的帖子: 记录 
2013-05-15 22:47
我是胖子
Rank: 1
等 级:新手上路
帖 子:20
专家分:4
注 册:2013-5-12
收藏
得分:0 
project.rar (2.14 KB)
代码文件已发

主要问题是

AddItem()函数
添加记录后

ScanItem()
不能显示新添加的记录


[ 本帖最后由 我是胖子 于 2013-5-16 10:12 编辑 ]
2013-05-15 22:48
我是胖子
Rank: 1
等 级:新手上路
帖 子:20
专家分:4
注 册:2013-5-12
收藏
得分:0 
不能解决么?
2013-05-16 06:40
Ryker
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:145
专家分:420
注 册:2013-2-19
收藏
得分:10 
我试过你这代码..一步一步跟踪..ab+ 的时候就是错误

如果用a+b,是可以写入文件的,但是scanitem读不到,有点莫名其妙...
2013-05-16 09:07
我是胖子
Rank: 1
等 级:新手上路
帖 子:20
专家分:4
注 册:2013-5-12
收藏
得分:0 
回复 4楼 Ryker
ab+ 也可以写入       文件里有新内容     不知道为啥?    大神   你可不能丢下我不管啊
2013-05-16 09:16
baijianmin
Rank: 2
等 级:论坛游民
帖 子:16
专家分:44
注 册:2012-9-5
收藏
得分:10 
void Initialize()中的save(fp,i
这里多存了一个空的book类型,
改为save(fp,i-1);
2013-05-16 17:46
baijianmin
Rank: 2
等 级:论坛游民
帖 子:16
专家分:44
注 册:2012-9-5
收藏
得分:0 
或者save函数里头的for(j=0;j<=i;j++)
改成for(j=0;j<i;j++)
2013-05-16 17:55
快速回复:一个小程序,添加一条记录写入文件后,不能显示出来
数据加载中...
 
   



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

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