| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4683 人关注过本帖
标题:fopen返回错误指针,新人求助!!。。急。。
只看楼主 加入收藏
lx677677
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2011-5-31
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
    long num;
    char name[30];
    long stu_num;
    char stu_name[10];
    int lent_out;
   
    node *next;
    node *pioneer;
}book,*pbook;

int count=0;
FILE *fp;
pbook head;

void insert_newbook(pbook head);      
void delete_book(pbook head);         
void lendout_book(pbook head);         
void return_book(pbook head);         
void search_book(pbook head);         
void printf_bookinfo(pbook head);      
void reserve_book(pbook head,int count);   
void warning(pbook head,int count);        


int creat_bookinfofile()
{
    //fp=fopen("librarymaster.dat","a+");
    if((fp=fopen("librarymaster.dat","a+"))!=NULL)
        return 1;
    else
        return 0;
}

int main(void)
{
    int choice;
    pbook p,pdelete;
    char a[4],bookname[30],studentname[10];
    int newornot=0,booknum,studentnum,lentout;

    head=(pbook)malloc(sizeof(book));
    head->next=NULL;
    head->pioneer=NULL;
    head->num=0;

    if(creat_bookinfofile())
        printf("文件打开成功");
    else
    {
        printf("打开失败,系统将关闭");
        goto exitbookmaster;
    }
    if(newornot)
    {
        p=head->next;
        while(!feof(fp))
        {
            fscanf(fp,"%d%s%d%s%d",&booknum,bookname,&studentnum,studentname,&lentout);
            p->num=booknum;
            strcpy(p->name,bookname);
            p->stu_num=studentnum;
            strcpy(p->stu_name,studentname);
            p->lent_out=lentout;
            count++;
        }
    }

    if(!fclose(fp))
    {
        printf("关闭文件失败,强制退出程序\n");
        system("pause");
        //goto exitbookmaster;
        exit(0);
    }

    do{
   
        printf("╔══════════════════════════════════╗");
        printf("║                    欢迎使用图书管理系统㊣by 罗枭                   ║");
        printf("╠══════════════════════════════════╣");
        printf("║1、新增图书信息                                      2、删除图书信息║");
        printf("║3、图书借出                                          4、图书归还    ║");
        printf("║5、搜索图书                                          6、显示图书信息║");
        printf("║7、保存图书信息                                      0、退出管理系统║");
        printf("╚══════════════════════════════════╝");
   
        scanf("%d",&choice);

        switch(choice)
        {
        case 1:insert_newbook(head);       break;
        case 2:delete_book(head);          break;
        case 3:lendout_book(head);         break;
        case 4:return_book(head);          break;
        case 5:search_book(head);          break;
        case 6:printf_bookinfo(head);      break;
        case 7:reserve_book(head,count);   break;
        case 0:warning(head,count);        break;
        }

        system("cls");
        printf("您刚才已进行操作 d%\n",choice);

    }while(choice!=0);

    fclose(fp);
   
    printf("释放内存中,图书管理系统即将关闭");
   
    pdelete=head;
    while(pdelete!=NULL)
    {
        free(pdelete->pioneer);
        pdelete=pdelete->next;
    }
    exitbookmaster:;
    system("pause");
    return 0;
}



void insert_newbook(pbook head)
{
    long booknum,studentnum;
    char bookname[30],studentname[10];
    int lentout;
    pbook newbook,ptr,ptr1,ptr2;

    insertagain:

    printf("请按顺序分别输入 新书号、新书名)");
    scanf("%ld%s%ld%s",&booknum,bookname);
   
    newbook->num=booknum;
    strcpy(newbook->name,bookname);
    newbook->stu_num=0;
    strcpy(newbook->stu_name,"无");
    newbook->lent_out=0;

    ptr=head->next;
    while(1)
    {
        ptr=ptr->next;

        if( (newbook->num) > (ptr->pioneer->num) || (newbook->num) < (ptr->num) )
        {
            newbook->pioneer=ptr->pioneer;
            newbook->next=ptr;
            ptr->pioneer->next=newbook;
            ptr->pioneer=newbook;

            break;
        }
        else
        {
            newbook->pioneer=ptr;
            newbook->next=NULL;
            ptr->next=newbook;

            break;
        }
    }
    count++;
    {
        int choice1;
        printf("继续输入请输入1,退出输入请输入0");
        scanf("%d",&choice1);
        if(choice1)
            goto insertagain;
    }
}

void delete_book(pbook head)
{
    char bookname[30];
    long booknum;
    int choice,a;
    pbook pdelete;

    while(1)
    {
        printf("请选择删除方式");
        printf("1、按书号删除");
        printf("2、按书名删除(将会删除所有同名的书本信息)");
        printf("0、退出");
        scanf("%d",&choice);

        if(choice==1)
        {
            printf("请输入书号:\n");
            scanf("%ld",&booknum);

            for(pdelete=head;pdelete;pdelete=pdelete->next)
            {
                if(booknum==pdelete->num)
                {
                    pdelete->pioneer->next=pdelete->next;
                    pdelete->next->pioneer=pdelete->pioneer;
                    free(pdelete);
                    count--;
                }
            }
            if(pdelete==NULL)
                printf("无此书号,请确认后继续查询,退出查询请输入“0”");
            scanf("%d",&a);
            if(!a)
                break;
        }

        else if(choice==2)
        {
            printf("请输入书名:\n");
            scanf("%s",bookname);

            for(pdelete=head;pdelete;pdelete=pdelete->next)
            {
                if(strcmp(bookname,pdelete->name)==0)
                {
                    pdelete->pioneer->next=pdelete->next;
                    pdelete->next->pioneer=pdelete->pioneer;
                    free(pdelete);
                    count--;
                }
            }
            if(pdelete==NULL)
                printf("无此书号,请确认后继续查询,退出查询请输入“0”");
            scanf("%d",&a);
            if(!a)
                break;
        }
        else
            break;
    }
}

void lendout_book(pbook head)
{
    long booknum,studentnum;
    char studentname[10];
    pbook ptr=head->next;

    lentoutagain:
    printf("请输入借出的书号:\n");
    scanf("%ld",&booknum);
    printf("请输入学号、姓名");
    scanf("%ld%s",&studentnum,studentname);

    if( (ptr->num) < (booknum) )
    {
        while( ptr->num!=booknum )
        {
            ptr=ptr->next;
        }
    }
    else if( (ptr->num) > (booknum))
    {
        while( ptr->num!=booknum )
        {
            ptr=ptr->pioneer;
        }
    }
    else
    {
        printf("输入书号有错,请重新输入\n");
        goto lentoutagain;
    }
    ptr->stu_num=studentnum;
    strcpy(ptr->stu_name,studentname);
    ptr->lent_out=1;

    {
        int chioce;
        printf("继续借出请输入1,退出输入0");

        if(chioce)
            goto lentoutagain;
    }
}

void return_book(pbook head)
{
    long booknum;
    pbook ptr;
    ptr=head;

    returnagain:
    printf("请输入要归还的书号");
    scanf("%ld",&booknum);

    if( (ptr->num) < (booknum) )
    {
        while( ptr->num!=booknum )
        {
            ptr=ptr->next;
        }
    }
    else if( (ptr->num) > (booknum))
    {
        while( ptr->num!=booknum )
        {
            ptr=ptr->pioneer;
        }
    }
    else
    {
        printf("输入书号有错,请重新输入\n");
        goto returnagain;
    }
    strcpy(ptr->stu_name,"null");
    ptr->stu_num=0;
    ptr->lent_out=0;

    {
        int choice;
        printf("继续还书请输入1,退出输入0");
        scanf("%ld",&choice);
        if(choice)
            goto returnagain;
    }

}

void search_book(pbook head)
{
    int choice;
    pbook ptr=head;
    char bookname[30];
    long booknum;

    searchagain:
    printf("请输入查找方式");
    printf("1、按书号查找");
    printf("2、按书名查找");
    printf("0、退出");
    scanf("%d",&choice);

    if(choice==1)
    {
        scanf("%ld",&booknum);
        if( (ptr->num) < (booknum) )
        {
            while( ptr->num!=booknum )
            {
                ptr=ptr->next;
                if(!ptr)
                {
                    printf("输入书号有误,重新查找请输入1,退出输入0");
                    {
                        int choice1;
                        printf("继续查询输入1,退出查询输入0");
                        scanf("%d",&choice);
                        if(choice1)
                            goto searchagain;
                        else
                            goto exitsearch;
                    }
                }
            }
        }
        else if( (ptr->num) > (booknum))
        {
            while( ptr->num!=booknum )
            {
                ptr=ptr->pioneer;
                if(!ptr)
                {
                    printf("输入书号有误,重新查找请输入1,退出输入0");
                    {
                        int choice1;
                        printf("继续查询输入1,退出查询输入0");
                        scanf("%d",&choice);
                        if(choice1)
                            goto searchagain;
                        else
                            goto exitsearch;
                    }
                }
            }
        }

        printf("%ld%3.30s%4.ld%3.10s%2.d",ptr->num,ptr->name,ptr->stu_num,ptr->stu_name,ptr->lent_out);

        {
            int choice1;
            printf("继续查询输入1,退出查询输入0");
            scanf("%d",&choice);
            if(choice1)
                goto searchagain;
        }
    }

    else if(choice==2)
    {
        scanf("%s",bookname);
        ptr=head;
        while( strcmp(ptr->name,bookname)!=0 )
        {
            ptr=ptr->next;
            if(!ptr)
            {
                printf("输入书号有误,重新查找请输入1,退出输入0");
                {
                    int choice1;
                    printf("继续查询输入1,退出查询输入0");
                    scanf("%d",&choice);
                    if(choice1)
                        goto searchagain;
                    else
                        goto exitsearch;
                }
            }
        }
        printf("%ld%3.30s%4.ld%3.10s%2.d",ptr->num,ptr->name,ptr->stu_num,ptr->stu_name,ptr->lent_out);
        {
            int choice1;
            printf("继续查询输入1,退出查询输入0");
            scanf("%d",&choice);
            if(choice1)
                goto searchagain;
        }
    }
    else
        goto exitsearch;
exitsearch:;
}

void printf_bookinfo(pbook head)
{
    pbook ptr=head->next;
    while(ptr)
    {
        printf("%ld%3.30s%4.ld%3.10s%2.d\n",ptr->num,ptr->name,ptr->stu_num,ptr->stu_name,ptr->lent_out);
        ptr=ptr->next;
    }
}

void reserve_book(pbook head,int count)
{
    FILE *fp;
    fp=fopen("librarymaster.dat","ab+");
    if(!fp)
    {
        printf("打开程序失败,结束保存。");
        goto exitreserve;
        exit(0);
    }
    fwrite(head,sizeof(book),count,fp);
    exitreserve:;
}

void warning(pbook head,int count)
{
    printf("程序将退出,是否保存?\n 1、是 0、否");
    int choice;
    scanf("%d",&choice);
    if(choice)
        reserve_book(head,count);
}
2011-05-31 12:22
chenqiu111
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-3-31
收藏
得分:0 
楼主问题解决了吗?遇到了相同的问题求助~~
2014-03-31 09:40
快速回复:fopen返回错误指针,新人求助!!。。急。。
数据加载中...
 
   



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

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