当前位置: 代码迷 >> C语言 >> 求教 删除文件中一段内容的问题~~~高手们帮忙看看啊~~
  详细解决方案

求教 删除文件中一段内容的问题~~~高手们帮忙看看啊~~

热度:399   发布时间:2008-06-03 20:34:21.0
求教 删除文件中一段内容的问题~~~高手们帮忙看看啊~~
//其他功能都是好的,就是那个删除函数有问题啊
//是不是删除的方法有问题?该怎么弄啊?


/******************************************************************************************
设一个通信录由以下几项数据信息构成:
数据项               类型

姓名                  字符串
地址                  字符串
邮政编码              字符串
电话号码              字符串
试为通信录数据定义类型和定义通信录变量, 实现功能:存取、显示、删除记录、查询记录等功能。
******************************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(AddressList)


typedef  struct
{
    char name[30];
    char address[30];
    char postalcode[30];
    char phone[30];
}AddressList;

AddressList list;

void PrintMassage()
{
    printf("***********************************************\n");
    printf("*                                             *\n");
    printf("*                通讯录管理系统               *\n");
    printf("*                                             *\n");
    printf("***********************************************\n");
    printf("\n===========================================\n\n");
}
int ShowMenu()        
{
    int choice;

    printf("1.显示通讯录...\n");
    printf("2.新增记录...\n");
    printf("3.删除记录...\n");
    printf("4.查询记录...\n");
    printf("5.退出程序...\n\n");
    printf("选择( ? )\b\b\b");

    scanf("%d",&choice);

    fflush(stdin);    

    return(choice);
}

void ShowAddressList()    
{
    FILE *fp;

    if ((fp = fopen("addresslist.dat","rb")) == NULL)        
    {
        printf("file open error!\n");
        printf("可能通讯录不存在,请选新增记录来建立一个新的通讯录...");
        return;
    }
    printf("\n姓名       地址                 邮政编码         电话号码      \n");
    while (!feof(fp))
    {
        //这个循环里面有3种方式写,其中1是错的,2,3是对的--怎么回事啊?
        //--1
        //--fread(&list,LEN,1,fp);
        //--printf("%-10s %-20s %-16s %-14s\n",list.name,list.address,list.postalcode,list.phone);
        //--2
        //--if(fread(&list,LEN,1,fp) == 1)
        //--printf("%-10s %-20s %-16s %-14s\n",list.name,list.address,list.postalcode,list.phone);
        //--3
        //--printf("%-10s %-20s %-16s %-14s\n",list.name,list.address,list.postalcode,list.phone);
        //--fread(&list,LEN,1,fp);
        printf("%-10s %-20s %-16s %-14s\n",list.name,list.address,list.postalcode,list.phone);
        fread(&list,LEN,1,fp);
    }
    printf("\n");
    fclose(fp);
}
void AddList()        //增加记录
{
    FILE *fp;

    if ((fp = fopen("addresslist.dat","ab")) == NULL)        //以追加的方式打开通讯录文件
    {
        printf("file open error!\n");
        return;
    }
    printf("\n请输入新增的记录...\n");
    printf("请按如下顺序输入并在每项输入完毕后以回车或空格结束\n姓名 地址 邮政编码 电话号码:\n");
    scanf("%s%s%s%s",list.name,list.address,list.postalcode,list.phone);
    fflush(stdin);
    //scanf("%s\%s\%s\%s",list.name,list.address,list.postalcode,list.phone);

    if (fwrite(&list,LEN,1,fp) != 1)
    {
        printf("文件写入错误!\n");
        return;
    }
    else
        printf("新增记录成功!\n");
    printf("\n");

    fclose(fp);
}
void SearchList()        //查询记录
{
    FILE *fp;
    char word[30];

    if ((fp = fopen("addresslist.dat","rb")) == NULL)
    {
        printf("通讯录打开失败!\n");
        return;
    }
    printf("请输入要查找的人的姓名...\n?");
    //scanf("%s",word);
    gets(word);

    do
    {
        fread(&list,LEN,1,fp);
    } while ((strcmp(list.name,word) != 0) && !feof(fp));

    if ((strcmp(list.name,word) != 0) && feof(fp))
        printf("没有在通讯录中找到记录...");
    else
    {
        printf("\n成功查找到记录!\n");
        printf("\n姓名       地址                 邮政编码        电话号码      \n");
        printf("%-10s %-20s %-16s %-14s\n",list.name,list.address,list.postalcode,list.phone);
    }
    printf("\n");

    fclose(fp);
}
void DelList()
{
    long j;
    char c;
    FILE *fp;
    FILE *temp;        //声明一个临时文件指针
    char del[30];

    if (temp = fopen("temp.dat","wb+") == NULL)        //以读写的方式新建一个2进制文件
    {
        printf("系统错误!临时文件创建失败!");
        exit(0);
    }
    if ((fp = fopen("addresslist.dat","rb+")) == NULL)
    {
        printf("通讯录打开失败!\n");
        return;
    }
    printf("请输入要删除的记录的姓名:");
    gets(del);

/*-----下面开始就有问题,帮忙看看啊-----*/


    do
    {
        fread(&list,LEN,1,fp);
        j = ftell(fp);
    } while ((strcmp(list.name,del) != 0) && !feof(fp));

    if ((strcmp(list.name,del) != 0) && feof(fp))
        printf("欲删除的记录在通讯录中不存在...\n");
    else
    {
        if (j = -1)
        {    
            printf("\nerror\n\n");
            return;
        }
    
        while (!feof(fp))//将欲删除记录后面的数据写入临时文件temp.dat
        {
            fread(temp,LEN,1,fp);

            if (fwrite(&list,LEN,1,temp) != 1)
            {
                printf("文件写入错误!\n");
                return;
            }

        }
    }

    fseek(fp,j-LEN,0);
    rewind(temp);

    while (!feof(temp))        //将临时文件以覆盖掉欲删除的记录的方式写入通讯录
    {
        c = fgetc(temp);
        fputc(c,fp);
    }

    fclose(fp);
    fclose(temp);

    printf("删除记录成功!");

    if (temp = fopen("temp.dat","wb") == NULL)        //清空临时文件
    {
        printf("系统错误!清空临时文件失败!");
        exit(0);
    }
    fclose(temp);
}



int main(void)
{
    int choice;

    PrintMassage();
    while (1)
    {    
        if ((choice = ShowMenu()) !=5)
        {
            switch (choice)
            {
            case 1: ShowAddressList();
                break;
            case 2: AddList();
                break;
            case 3:DelList();
                break;
            case 4:SearchList();
                break;
            }
        }
        if (choice == 5)
            break;
    }
    
    system("pause");
    return 0;
}
搜索更多相关的解决方案: 邮政编码  文件  include  字符  

----------------解决方案--------------------------------------------------------
  相关解决方案