大婉猫 2022-12-17 22:12 采纳率: 0%
浏览 83
已结题

文件输入无法存储信息

问题遇到的现象和发生背景

无法储存信息写入文件

遇到的现象和发生背景,请写出第一个错误信息

img

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#pragma warning(disable:4996)
#include<stdio.h>
#include<string.h>
#include <iostream>
#define BookSize 100
#define BLHum 50
#define BLHnum 50
#define RRnum 50
using namespace std;
/*主数据库文件*/
typedef struct {
    int bno;
    char bname[21];
    int namenext;
    char author[9];
    int authnext;
    char press[11];
    int prenext;
    char sortno[4];
    int storenum;
    int borrownum;
}BookRecType;
typedef struct {
    BookRecType BookDbase[BookSize];
    int len;
}BookDbaseFile;


/*书号索引文件*/
typedef struct {
    int bno;
    int RecNo;
}BidxRecType;
typedef struct {
    BidxRecType BnoIdx[BookSize];
    int len;
}BnoIdxFile;


/*书名链头索引文件*/
typedef struct {
    char bname[21];
    int lhead;
    int RecNum;
}BNRecType;
typedef struct {
    BNRecType LHFrecl[BLHum];
    int len1;
}LHFile1;


/*作者链头索引文件*/
typedef struct {
    char author[9];
    int lhead;
    int RecNum;
}BARecType;
typedef struct {
    BARecType LHFrec2[BLHnum];
    int len2;
}LHFile2;


/*出版社链头索引问件*/
typedef struct {
    char press[11];
    int lhead;
    int RecNum;
}BPRecType;
typedef struct {
    BPRecType LHFrec3[BLHnum];
    int len3;
}LHFile3;


/*读者文件*/
typedef struct {
    int rno;
    char name[8];
    int bn1;
    int bn2;
}RRecType;
typedef struct {
    RRecType ReadRec[RRnum];
    int len;
}ReadFile;


/*借还书文件*/
typedef struct {
    int rno;
    int bno;
    char date1[9];
    char date2[9];
}BbookRecType;
typedef struct {
    BbookRecType Bbook[BookSize];
    int len;
}BbookFile;


/*追加一条图书记录建立相关文件*/
BookDbaseFile AppeDBaseRec(BookDbaseFile df)
{
    int i = 0;
    printf("书号    书名    作者名    出版社    分类    藏书量\n");
    cin >> df.BookDbase[i].bno >> df.BookDbase[i].bname;
    cin >> df.BookDbase[i].author >> df.BookDbase[i].press;
    cin >> df.BookDbase[i].sortno >> df.BookDbase[i].storenum;
    df.BookDbase[i].borrownum = 0;
    return df;
}

/*修改书号索引表的设计*/
BnoIdxFile ChangeBnoIdxF(BookDbaseFile df, BnoIdxFile bif)
{
    int i, j, k = 1;
    i = df.len;
    j = bif.len;
    while (j >= 1)
    {
        if (df.BookDbase[i].bno > bif.BnoIdx[j].bno)
        {
            k = j + 1;
            break;
        }
        j--;
    }
    if (bif.len > 0)
        for (j = bif.len; j >= k; j--)
            bif.BnoIdx[j + 1] = bif.BnoIdx[j];
    bif.BnoIdx[k].bno = df.BookDbase[i].bno;
    bif.BnoIdx[k].RecNo = i;
    bif.len++;
    return bif;
}


/*修改书名索引以及书名链头索引的设计*/
LHFile1 ChangeLinkHeadF1(BookDbaseFile* df, LHFile1 lhf1)
{
    int i, j, k, m;
    char sm[21];
    i = df->len;
    strcpy(sm, df->BookDbase[i].bname);
    j = 1;
    k = 0;
    while (j <= lhf1.len1)
    {
        if (strcmp(sm, lhf1.LHFrecl[j].bname) == 0)
        {
            k = j;
            break;
        }
        j++;
    }
    if (k != 0)
    {
        df->BookDbase[i].namenext = lhf1.LHFrecl[k].lhead;
        lhf1.LHFrecl[k].lhead = i;
        lhf1.LHFrecl[k].RecNum++;
    }
    else
    {
        m = ++lhf1.len1;
        df->BookDbase[i].namenext = 0;
        lhf1.LHFrecl[m].lhead = i;
        lhf1.LHFrecl[m].RecNum = 1;
        strcpy(lhf1.LHFrecl[m].bname, sm);
    }
    return lhf1;
}


/*修改作者索引以及作者链头索引表的设计*/
LHFile2 ChangeLinkHeadF2(BookDbaseFile* df, LHFile2 lhf2)
{
    int i, j, k, m = 0;
    char zz[9];
    i = df->len;
    strcpy(zz, df->BookDbase[i].author);
    j = 1;
    k = 0;
    while (j <= lhf2.len2)
    {
        if (strcmp(zz, lhf2.LHFrec2[j].author) == 0)
        {
            k = j;
            break;
        }
        j++;
    }
    if (k != 0)
    {
        df->BookDbase[i].authnext = lhf2.LHFrec2[k].lhead;
        lhf2.LHFrec2[k].lhead = i;
        lhf2.LHFrec2[k].RecNum = 1;
        strcpy(lhf2.LHFrec2[m].author, zz);
    }
    return lhf2;
}


/*修改出版社的索引以及出版社链头索引表的设计*/
LHFile3 ChangeLinkHeadF3(BookDbaseFile* df, LHFile3 lhf3)
{
    int i, j, k, m;
    char cbs[11];
    i = df->len;
    strcpy(cbs, df->BookDbase[i].press);
    j = 1;
    k = 0;
    while (j <= lhf3.len3)
    {
        if (strcmp(cbs, lhf3.LHFrec3[j].press) == 0)
        {
            k = j;
            break;
        }
        j++;
    }
    if (k != 0)
    {
        df->BookDbase[i].prenext = lhf3.LHFrec3[k].lhead;
        lhf3.LHFrec3[k].lhead = i;
        lhf3.LHFrec3[k].RecNum++;
    }
    else
    {
        m = ++lhf3.len3;
        df->BookDbase[i].prenext = 0;
        lhf3.LHFrec3[m].lhead = i;
        lhf3.LHFrec3[m].RecNum = i;
        strcpy(lhf3.LHFrec3[m].press, cbs);
    }
    return lhf3;
}


/*用二分查找实现书号查询算法*/
int BinSearch(BnoIdxFile bif, int key)
{
    int low, high, mid;
    low = 1;
    high = bif.len;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (key == bif.BnoIdx[mid].bno)
            return bif.BnoIdx[mid].RecNo;
        else
            if (key < bif.BnoIdx[mid].bno)
                high = mid - 1;
            else
                low = mid + 1;
    }
    return 0;
}


/*按书名查询算法的设计*/
int BnameFind(LHFile1 lhf1, char key[])
{
    int i, k = 0;
    for (i = 1; i <= lhf1.len1; i++)
    {
        if (strcmp(key, lhf1.LHFrecl[1].bname) == 0)
        {
            k = lhf1.LHFrecl[i].lhead;
            break;
        }
    }
    return k;
}


/*按作者查询算法的设计*/
int BauthFind(LHFile2 lhf2, char key[])
{
    int i, k = 0;
    for (i = 1; i <= lhf2.len2; i++)
    {
        if (strcmp(key, lhf2.LHFrec2[i].author) == 0)
        {
            k = lhf2.LHFrec2[i].lhead;
            break;
        }
    }
    return k;
}

/*按出版社查询算法的设计*/
int BnameFind(LHFile3 lhf3, char key[])
{
    int i, k = 0;
    for (i = 1; i <= lhf3.len3; i++)
    {
        if (strcmp(key, lhf3.LHFrec3[i].press) == 0)
        {
            k = lhf3.LHFrec3[i].lhead;
            break;
        }
    }
    return k;
}

/*输出一条图书主数据库记录的设计*/
void ShowRec(BookDbaseFile df, int i)
{
    printf("书号    书名    作者名    出版社    分类号    可借数\n");
    printf("==============================================\n");
    printf("%d%12s", df.BookDbase[i].bno, df.BookDbase[i].bname);
    printf("%8s%12s", df.BookDbase[i].author, df.BookDbase[i].press);
    printf("%6s", df.BookDbase[i].sortno);
    printf("%4d\n", df.BookDbase[i].storenum - df.BookDbase[i].borrownum);
    printf("==============================================\n");
}

/*图书查询控制程序设计*/
void SearchBook(BookDbaseFile df, BnoIdxFile bif, LHFile1 f1, LHFile2 f2, LHFile3 f3)
{
    char sm[21], zz[9], cbs[11];
    int i, k, choose = 1;
    int sh;
    while (choose >= 1 && choose <= 5)
    {
        printf("图书查询子系统\n");
        printf("------------------\n");
        printf("1.书 号        2.书 名\n");
        printf("3.作 者        4.出版社\n");
        printf("5.退 出    查 询\n");
        printf("------------------\n");
        printf("    请 用 户 选 择:");
        cin >> choose;
        switch (choose)
        {
        case 1:
            printf("输入书号:");
            cin >> sh;
            k = BinSearch(bif, sh);
            if (k == 0) {
                printf("没有要检查的图书,请检查是否输入有错\n");
                break;
            }
            ShowRec(df, k);
            break;

        case 2:
            printf("输入书名:");
            cin >> sm;
            k = BnameFind(f1, sm);
            if (k == 0) {
                printf("没有要检查的图书,请检查是否输入有错\n");
                break;
            }
            for (i = k; i; i = df.BookDbase[i].namenext)
                ShowRec(df, i);
            break;

        case 3:
            printf("输入作者名:");
            cin >> zz;
            k = BauthFind(f2, zz);
            if (k == 0) {
                printf("没有要检查的图书,请检查是否输入有错\n");
                break;
            }
            for (i = k; i; i = df.BookDbase[i].authnext)
                ShowRec(df, i);
            break;

        case 4:
            printf("输入出版社:");
            cin >> cbs;
            k = BnameFind(f3, cbs);
            if (k == 0) {
                printf("没有要检查的图书,请检查是否输入有错\n");
                break;
            }
            for (i = k; i; i = df.BookDbase[i].prenext)
                ShowRec(df, k);
            break;

        case 5:
            return;
        }
    }
}


/*借还书处理算法*/
void BorrowBook(BookDbaseFile* bf, BnoIdxFile bif, BbookFile* bbf, ReadFile* rf)
{
    char jyrq[9];
    int sh, dzh;
    int i, j, k = 0;
    printf("输入读者号    书号    借阅日期\n");
    cin >> dzh >> sh >> jyrq;
    for (i = 1; i <= rf->len; i++)
    {
        if (dzh == rf->ReadRec[i].rno)
        {
            k = i;
            break;
        }
    }
    if (k == 0)
    {
        printf("非法读者!\n");
        return;
    }
    if (rf->ReadRec[k].bn2 >= rf->ReadRec[k].bn1)
    {
        printf("书已借满!\n");
        return;
    }
    j = BinSearch(bif, sh);
    if (j == 0)
    {
        printf("非法书号!");
        return;
    }
    if (bf->BookDbase[j].borrownum >= bf->BookDbase[j].storenum)
    {
        printf("图书已借出\n");
        return;
    }
    i = ++bbf->len;
    bbf->Bbook[i].rno = dzh;
    bbf->Bbook[i].bno = sh;
    strcpy(bbf->Bbook[i].date1, jyrq);
    rf->ReadRec[k].bn2++;
    bf->BookDbase[j].borrownum++;
    printf("借书成功!\n");
}

/*还书处理算法的设计*/
void BackBook(BookDbaseFile* bf, BnoIdxFile bif, BbookFile* bbf, ReadFile* rf)
{
    char hsrq[8];
    int sh, dzh;
    int i, j, k = 0, m = 0;
    printf("读者号    书号    还书日期\n");
    cin >> dzh >> sh >> hsrq;
    for (i = 1; i <= rf->len; i++)
    {
        if (dzh == rf->ReadRec[i].rno)
        {
            k = i;
            break;
        }
    }
    if (k == 0)
    {
        printf("非法读者!\n");
        return;
    }
    for (i = 1; i <= bbf->len; i++)
        if (sh == bbf->Bbook[i].bno)
        {
            m = i;
            break;
        }
    if (m == 0)
    {
        printf("非法书号!\n");
        return;
    }
    j = BinSearch(bif, sh);
    if (j == 0)
    {
        printf("非法书号!\n");
        return;
    }
    rf->ReadRec[k].bn2--;
    bf->BookDbase[j].borrownum--;
    strcpy(bbf->Bbook[m].date2, hsrq);
    printf("还书成功!\n");
}

/*读者管理子系统*/
ReadFile ReaderManage(ReadFile rf)
{
    int i;
    char yn = 'y';
    i = ++rf.len;
    while (yn == 'y' || yn == 'Y')
    {
        printf("输入读者号    读者名    可借图书数\n");
        cin >> rf.ReadRec[i].rno >> rf.ReadRec[i].name;
        cin >> rf.ReadRec[i].bn1;
        rf.ReadRec[i].bn2 = 0;
        printf("继续输入吗?y/n:");
        cin >> yn;
    }
    rf.len = i - 1;
    return rf;
}

/*写各类文件*/
void writefile(BookDbaseFile* bf, BnoIdxFile* bif, LHFile1* f1, LHFile2* f2, LHFile3* f3, ReadFile* rf, BbookFile* bbf)
{
    FILE* fp;
    int i;
    //图书主文件 
    fp = fopen("book", "w");
    for (i = 1; i <= bf->len; i++)
        fwrite(&bf->BookDbase[i], sizeof(BookRecType), 1, fp);
    fclose(fp);
    //图书索引文件 
    fp = fopen("bidx", "w");
    for (i = 1; i <= bif->len; i++)
        fwrite(&bif->BnoIdx[i], sizeof(BidxRecType), 1, fp);
    fclose(fp);
    //书名索引链头文件 
    fp = fopen("nidx", "w");
    for (i = 1; i <= f1->len1; i++)
        fwrite(&f1->LHFrecl[i], sizeof(BNRecType), 1, fp);
    fclose(fp);
    //作者索引链头文件
    fp = fopen("aidx", "w");
    for (i = 1; i <= f2->len2; i++)
        fwrite(&f2->LHFrec2[i], sizeof(BARecType), 1, fp);
    fclose(fp);
    //出版社索引链头文件
    fp = fopen("pidx", "w");
    for (i = 1; i <= f3->len3; i++)
        fwrite(&f3->LHFrec3[i], sizeof(BPRecType), 1, fp);
    fclose(fp);
    //读者文件
    fp = fopen("read", "w");
    for (i = 1; i <= rf->len; i++)
        fwrite(&rf->ReadRec[i], sizeof(RRecType), 1, fp);
    fclose(fp);
    //借还书文件
    fp = fopen("bbff", "w");
    for (i = 1; i <= bbf->len; i++)
        fwrite(&bbf->Bbook[i], sizeof(BbookRecType), 1, fp);
    fclose(fp);
}


/*读各类文件*/
void readfile(BookDbaseFile* bf, BnoIdxFile* bif, LHFile1* f1, LHFile2* f2, LHFile3* f3, ReadFile* rf, BbookFile* bbf)
{
    FILE* fp;
    int i;
    //读图书主文件
    fp = fopen("book", "rb");
    i = 1;
    while (!feof(fp))
    {
        fread(&bf->BookDbase[i], sizeof(BookRecType), 1, fp);
        i++;
        if (feof(fp))
            break;
    }
    bf->len = i - 2;
    fclose(fp);
    //读书好索引文件
    fp = fopen("bidx", "rb");
    i = 1;
    while (!feof(fp))
    {
        fread(&bif->BnoIdx[i], sizeof(BidxRecType), 1, fp);
        i++;
    }
    bif->len = i - 2;
    fclose(fp);
    //读书名索引文件
    fp = fopen("nidx", "rb");
    i = 1;
    while (!feof(fp))
    {
        fread(&f1->LHFrecl[i], sizeof(BNRecType), 1, fp);
        i++;
    }
    f1->len1 = i - 2;
    fclose(fp);
    //读作者索引文件
    fp = fopen("aidx", "rb");
    i = 1;
    while (!feof(fp))
    {
        fread(&f2->LHFrec2[i], sizeof(BARecType), 1, fp);
        i++;
    }
    f2->len2 = i - 2;
    fclose(fp);
    //读出版社索引链头文件
    fp = fopen("pidx", "rb");
    i = 1;
    while (!feof(fp))
    {
        fread(&f3->LHFrec3[i], sizeof(BPRecType), 1, fp);
        i++;
    }
    f3->len3 = i - 2;
    fclose(fp);
    //读读者文件
    fp = fopen("read", "rb");
    i = 1;
    while (!feof(fp))
    {
        fread(&rf->ReadRec[i], sizeof(RRecType), 1, fp);
        i++;
    }
    rf->len = i - 2;
    fclose(fp);
    //读借还书文件
    fp = fopen("bbff", "rb");
    i = 1;
    while (!feof(fp))
    {
        fread(&bbf->Bbook[i], sizeof(BbookRecType), 1, fp);
        i++;
    }
    bbf->len = i - 2;
    fclose(fp);
}//

/*主控菜单*/
int main(void)
{
    int j, m, k = 1;
    char xz = 'n';
    BookDbaseFile bf;
    BnoIdxFile bif;
    LHFile1 f1;
    LHFile2 f2;
    LHFile3 f3;
    ReadFile rf;
    BbookFile bbf;
    memset(&bf, 0, sizeof(BookDbaseFile));
    memset(&bif, 0, sizeof(BnoIdxFile));
    memset(&f1, 0, sizeof(LHFile1));
    memset(&f2, 0, sizeof(LHFile2));
    memset(&f3, 0, sizeof(LHFile3));
    memset(&rf, 0, sizeof(ReadFile));
    memset(&bbf, 0, sizeof(BbookFile));
    while ((k <= 5) && (k > 0))
    {
        printf("图书管理系统\n");
        printf("============\n");
        printf(" 1.系统维护 \n");
        printf(" 2.读者管理 \n");
        printf(" 3.图书管理 \n");
        printf(" 4.图书流通 \n");
        printf(" 5.退出系统 \n");
        printf("============\n");
        printf(" 请选择1-5:");
        cin >> k;
        switch (k)
        {
        case 1:
            printf("系统维护\n");
            printf("--------\n");
            printf("1.初始化\n");
            printf("2. 读 盘\n");
            printf("--------\n");
            printf("请选择:\n");
            cin >> m;
            switch (m)
            {
            case 1:
                printf("初始化只能做一次,需慎重!初始化吗?y/n:");
                cin >> xz;
                if (xz == 'y' || xz == 'Y')
                {
                    bf.len = bif.len = f1.len1 = f2.len2 = 0;
                    f3.len3 = rf.len = bbf.len = 0;
                }
                break;

            case 2:
                readfile(&bf, &bif, &f1, &f2, &f3, &rf, &bbf);
                break;
            }
            break;

        case 2:
            ReaderManage(rf);
            break;

        case 3:
            printf("图书管理子系统\n");
            printf("--------------\n");
            printf("1.图书信息输入\n");
            printf("2.图书信息查询\n");
            printf("--------------\n");
            printf("  请 选 择 :");
            cin >> j;
            if (j == 1)
            {
                char yn = 'y';
                int k = 1;
                while (yn == 'y' || yn == 'Y')
                {
                    bf = AppeDBaseRec(bf);
                    bif = ChangeBnoIdxF(bf, bif);
                    f1 = ChangeLinkHeadF1(&bf, f1);
                    f2 = ChangeLinkHeadF2(&bf, f2);
                    f3 = ChangeLinkHeadF3(&bf, f3);
                    ShowRec(bf, k);
                    k++;
                    printf("继续输入吗?y/n:");
                    cin >> yn;
                }
            }
            else
                SearchBook(bf, bif, f1, f2, f3);
            break;

        case 4:
            printf("图书流通子系统\n");
            printf("--------------\n");
            printf(" 1. 借书处理  \n");
            printf(" 2. 还书处理  \n");
            printf("--------------\n");
            printf("  请 选 择 :  \n");
            cin >> j;
            if (j == 1)
                BorrowBook(&bf, bif, &bbf, &rf);
            else if (j == 2)
                BackBook(&bf, bif, &bbf, &rf);
            break;

        case 5:
            printf("系统正在写盘,请稍后......\n");
            writefile(&bf, &bif,&f1, &f2, &f3, &rf, &bbf);
            printf("再见!\n");
            return 0;
        }

    }
}break;

运行结果及详细报错内容

img


没有报错警告,就是不行

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%

我怀疑是写入文件那里出错,但改不对

我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”[]([](

系统功能可正确使用,能写入信息,

  • 写回答

3条回答 默认 最新

  • Jackyin0720 2022-12-17 23:07
    关注

    利用这个函数方法 fflush(stdout);rewind(stdin);

    1、在每个最后不带\n的printf后面加fflush(stdout);(对标准输出流进行清理,但是它并不是把数据丢掉,而是及时地打印数据到屏幕上。)
    2、在每个不想受到输入(接收)缓冲区旧内容影响的scanf();【或者getchar();或者gets_s();】前面加rewind(stdin);(rewind()函数的作用是把文件的当前位置指针指向文件头。)
        或者添加fflush(stdin);(fflush(stdin)刷新标准输入缓冲区,把输入缓冲区里的东西丢弃[非标准])
    3、另外请注意检查scanf的返回值。
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月18日
  • 赞助了问题酬金15元 12月17日
  • 创建了问题 12月17日

悬赏问题

  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密
  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?