C语言_文件操作相关练习题

当前文章列出了4个文件编程相关的练习题。文件拷贝实现、 文件加密、学生管理系统链表模板(未添加文件操作)、学生管理系统模板(通过文件系统保存信息)、等4个例子。

1.1 文件拷贝实现

#include 
#include 
#include 
int main()
{
    /*1. 打开源文件*/
    FILE *file_src;
    FILE *file_new;
    unsigned char buff[1024];
    unsigned int cnt;

    file_src=fopen("D:/123.pdf","rb");
    if(file_src==NULL)
    {
        printf("源文件打开失败!
");
        return -1;
    }
    /*2. 创建新文件*/
    file_new=fopen("D:/456.pdf","wb");
    if(file_new==NULL)
    {
        printf("新文件创建失败!
");
        return -1;
    }
    /*3. 拷贝文件*/
    while(!feof(file_src))
    {
        cnt=fread(buff,1,1024,file_src);
        fwrite(buff,1,cnt,file_new);
    }
    /*4. 关闭文件*/
    fclose(file_new);
    fclose(file_src);
    printf("文件拷贝成功!
");
    return 0;
}

1.2 文件加密

使用: 异或 ^ 密码类型: (1) 整型密码 (2) 字符串密码 比如: 银行提款机的密码、QQ密码 加密代码:

#include 
#include 
#include 
int main()
{
    /*1. 打开源文件*/
    FILE *file_src;
    FILE *file_new;
    unsigned char buff[1024];
    unsigned int cnt;
    unsigned int password=123456; //密码数据
    unsigned int data;  //存放读取的数据

    file_src=fopen("D:/123.pdf","rb");
    if(file_src==NULL)
    {
        printf("源文件打开失败!
");
        return -1;
    }
    /*2. 创建新文件*/
    file_new=fopen("D:/456.pdf","wb");
    if(file_new==NULL)
    {
        printf("新文件创建失败!
");
        return -1;
    }
    /*3. 文件加密*/
    while(!feof(file_src))
    {
        cnt=fread(&data,1,4,file_src);
        data=data^password;//文件数据加密
        fwrite(&data,1,cnt,file_new);
    }
    /*4. 关闭文件*/
    fclose(file_new);
    fclose(file_src);
    printf("文件加密成功!
");
    return 0;
}

解密代码:


 
#include 
#include 
#include 
int main()
{
    /*1. 打开源文件*/
    FILE *file_src;
    FILE *file_new;
    unsigned char buff[1024];
    unsigned int cnt;
    unsigned int password=123456; //密码数据
    unsigned int data;  //存放读取的数据

    file_src=fopen("D:/456.pdf","rb");
    if(file_src==NULL)
    {
        printf("源文件打开失败!
");
        return -1;
    }
    /*2. 创建新文件*/
    file_new=fopen("D:/789.pdf","wb");
    if(file_new==NULL)
    {
        printf("新文件创建失败!
");
        return -1;
    }
    /*3. 文件加密*/
    while(!feof(file_src))
    {
        cnt=fread(&data,1,4,file_src);
        data=data^password;//文件数据加密
        fwrite(&data,1,cnt,file_new);
    }
    /*4. 关闭文件*/
    fclose(file_new);
    fclose(file_src);
    printf("文件解密成功!
");
    return 0;
}

1.3 学生管理系统链表模板(未添加文件操作)


#include 
#include 
#include 

//存放信息的结构体
struct MyStruct
{
    char Name[50]; //存放姓名
    int Number;    //存放编号
    struct MyStruct *next; //存放下一个节点的地址
};

//链表相关的函数接口
struct MyStruct *ListHead=NULL; //链表头
struct MyStruct *CreateListHead(struct MyStruct *head);
void AddrListInfo(struct MyStruct *head,struct MyStruct data);
void DeleteListInfo(struct MyStruct *head,int number);
void PrintListAllInfo(struct MyStruct *head);


int main()
{
    struct MyStruct data1={"张三",123};
    struct MyStruct data2={"李四",456};
    struct MyStruct data3={"小王",789};
    ListHead=CreateListHead(ListHead);
    
    //添加信息
    AddrListInfo(ListHead,data1);
    AddrListInfo(ListHead,data2);
    AddrListInfo(ListHead,data3);

    //删除节点
    DeleteListInfo(ListHead,123);
    DeleteListInfo(ListHead,789);

    //打印
    PrintListAllInfo(ListHead);
    return 0;
}


/*
函数功能: 创建链表头
*/
struct MyStruct *CreateListHead(struct MyStruct *head)
{
    if(head==NULL)
    {
        head=malloc(sizeof(struct MyStruct));
        head->next=NULL;
    }
    return head;
}

/*
函数功能: 在链表结尾添加节点
*/
void AddrListInfo(struct MyStruct *head,struct MyStruct data)
{
    struct MyStruct *p=head;
    struct MyStruct *tmp=NULL;

    while(p->next!=NULL)
    {
        p=p->next;
    }
    tmp=malloc(sizeof(struct MyStruct));
    memcpy(tmp,&data,sizeof(struct MyStruct));
    p->next=tmp;
    tmp->next=NULL;
}

/*
函数功能: 根据结构体里特有的成员区分进行删除链表节点信息
函数参数: int numbe  编号
*/
void DeleteListInfo(struct MyStruct *head,int number)
{
    struct MyStruct *p=head;
    struct MyStruct *tmp=NULL;

    while(p->next!=NULL)
    {
        tmp=p; //保存上一个节点的信息
        p=p->next;
        if(p->Number==number)
        {
            tmp->next=tmp->next->next;
            free(p);
            p=head; //链表头归位
        }
    }
}

/*
函数功能: 打印所有节点信息
函数参数: int numbe  编号
*/
void PrintListAllInfo(struct MyStruct *head)
{
    struct MyStruct *p=head;
    int cnt=0;
    printf("
链表全部信息如下:
");
    while(p->next!=NULL)
    {
        p=p->next;
        cnt++;
        printf("第%d个节点信息: %s,%d
",cnt,p->Name,p->Number);
    }
}

1.4 学生管理系统模板(通过文件系统保存信息)


#include 
#include 
#include 

//存放信息的结构体
struct MyStruct
{
    char Name[50]; //存放姓名
    int Number;    //存放编号
    struct MyStruct *next; //存放下一个节点的地址
};

//链表相关的函数接口
struct MyStruct *ListHead=NULL; //链表头
struct MyStruct *CreateListHead(struct MyStruct *head);
void AddrListInfo(struct MyStruct *head,struct MyStruct data);
void DeleteListInfo(struct MyStruct *head,int number);
void PrintListAllInfo(struct MyStruct *head);

//文件操作相关函数
void SaveListAllInfo(struct MyStruct *head,char *path);
void GetListAllInfo(struct MyStruct *head,char *path);

#if 0
int main()
{
    struct MyStruct data1={"张三",123};
    struct MyStruct data2={"李四",456};
    struct MyStruct data3={"小王",789};
    ListHead=CreateListHead(ListHead);
    
    AddrListInfo(ListHead,data1);
    AddrListInfo(ListHead,data2);
    AddrListInfo(ListHead,data3);

    //保存节点信息
    SaveListAllInfo(ListHead,"D:/list.ini");

    //打印
    PrintListAllInfo(ListHead);
    return 0;
}
#endif

#if 1
int main()
{
    ListHead=CreateListHead(ListHead);

    //获取节点信息
    GetListAllInfo(ListHead,"D:/list.ini");

    //打印
    PrintListAllInfo(ListHead);
    return 0;
}
#endif

/*
函数功能: 创建链表头
*/
struct MyStruct *CreateListHead(struct MyStruct *head)
{
    if(head==NULL)
    {
        head=malloc(sizeof(struct MyStruct));
        head->next=NULL;
    }
    return head;
}

/*
函数功能: 在链表结尾添加节点
*/
void AddrListInfo(struct MyStruct *head,struct MyStruct data)
{
    struct MyStruct *p=head;
    struct MyStruct *tmp=NULL;

    while(p->next!=NULL)
    {
        p=p->next;
    }
    tmp=malloc(sizeof(struct MyStruct));
    memcpy(tmp,&data,sizeof(struct MyStruct));
    p->next=tmp;
    tmp->next=NULL;
}

/*
函数功能: 根据结构体里特有的成员区分进行删除链表节点信息
函数参数: int numbe  编号
*/
void DeleteListInfo(struct MyStruct *head,int number)
{
    struct MyStruct *p=head;
    struct MyStruct *tmp=NULL;

    while(p->next!=NULL)
    {
        tmp=p; //保存上一个节点的信息
        p=p->next;
        if(p->Number==number)
        {
            tmp->next=tmp->next->next;
            free(p);
            p=head; //链表头归位
        }
    }
}

/*
函数功能: 打印所有节点信息
函数参数: int numbe  编号
*/
void PrintListAllInfo(struct MyStruct *head)
{
    struct MyStruct *p=head;
    int cnt=0;
    printf("
链表全部信息如下:
");
    while(p->next!=NULL)
    {
        p=p->next;
        cnt++;
        printf("第%d个节点信息: %s,%d
",cnt,p->Name,p->Number);
    }
}


/*
函数功能: 保存链表节点信息
*/
void SaveListAllInfo(struct MyStruct *head,char *path)
{
    struct MyStruct *p=head;
    FILE *file;
    file=fopen(path,"a+b");
    if(file==NULL)
    {
        printf("保存信息的文件打开失败!
");
        return ;
    }
    while(p->next!=NULL)
    {
        p=p->next;
        fwrite(p,1,sizeof(struct MyStruct),file);
    }
    fclose(file);
}

/*
函数功能: 从文件里获取链表节点信息
*/
void GetListAllInfo(struct MyStruct *head,char *path)
{
    struct MyStruct *p=head;
    FILE *file;
    struct MyStruct data;

    file=fopen(path,"rb");
    if(file==NULL)
    {
        printf("保存信息的文件打开失败!
");
        return;
    }
    //循环读取文件里的数据
    while(!feof(file))
    {
        fread(&data,1,sizeof(struct MyStruct),file); //读取链表节点数据
        AddrListInfo(head,data); //添加链表节点
    }
    fclose(file);
}



展开阅读全文

页面更新:2024-05-21

标签:小王   文件   练习题   源文件   节点   函数   编号   密码   语言   操作   功能   数据   信息

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top