龙空技术网

C语言_文件IO操作函数总结

DS小龙哥 172

前言:

现在各位老铁们对“c语言中怎么调试方法”大概比较关注,朋友们都想要学习一些“c语言中怎么调试方法”的相关知识。那么小编也在网络上收集了一些对于“c语言中怎么调试方法””的相关资讯,希望你们能喜欢,各位老铁们快快来学习一下吧!

当前文章涉及C语言文件操作相关知识点。列出最常见的文件操作函数、fopen、fread、fwrite、fclose 等。通过几个常见需求,写出例子理解文件操作函数的用法。

1. 文件IO总结

文件IO操作:  对文件系统里的文件进行:  打开、创建、读、写、关闭等运用。C语言下标准文件IO接口(函数): (1)头文件:  stdio.h    输入输出函数:  printf 、scanf(2)相关函数:  fopen、fread、fwrite、fclose2.1 标准文件操作有两套函数:1.标准C语言下的文件操作接口。fopen系列常用于:  对普通文件的读写。2.Linux操作系统下的文件操作接口。open系列常用于:  对设备文件进行读写。 (鼠标、键盘、声卡、..)
2. C语言标准文件操作接口2.1 最常用的4个函数
#include <stdio.h>//打开文件FILE *fopen(const char *path, const char *mode); //读文件size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);//写文件size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);//关闭文件int fclose(FILE *fp);
2.3 写函数的基本运用
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    FILE *file;    int cnt;    /*1. 打开文件*/    file=fopen("D:/123.txt","a+b");    if(file==NULL)    {        printf("文件打开失败!\n");        return -1;    }    /*2. 写数据*/    cnt=fwrite("1234567890",1,10,file);        /*3. 关闭文件*/    fclose(file);    printf("cnt=%d\n",cnt);    return 0;}
2.4 读函数基本运用
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    FILE *file;    int cnt;    char buff[100];    /*1. 打开文件*/    file=fopen("D:/123.txt","rb"); //malloc    if(file==NULL)    {        printf("文件打开失败!\n");        return -1;    }    /*2. 写数据*/    cnt=fread(buff,1,100,file);        /*3. 关闭文件*/    fclose(file);  //free     buff[cnt]='\0';    printf("%s\n",buff);    printf("cnt=%d\n",cnt);    return 0;}
2.5 文件指针位置偏移 (自动向后偏移)
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    FILE *file;    int cnt;    char data;    /*1. 打开文件*/    file=fopen("D:/123.txt","rb"); //malloc    if(file==NULL)    {        printf("文件打开失败!\n");        return -1;    }    /*2. 读数据---验证文件指针是否可否自动向后偏移*/    cnt=fread(&data,1,1,file);    printf("data=%c\n",data);    cnt=fread(&data,1,1,file);    printf("data=%c\n",data);    cnt=fread(&data,1,1,file);    printf("data=%c\n",data);    cnt=fread(&data,1,1,file);    printf("data=%c\n",data);    cnt=fread(&data,1,1,file);    printf("data=%c\n",data);    /*3. 关闭文件*/    fclose(file);  //free     return 0;}
2.6 设置文件指针位置
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    FILE *file;    int cnt;    char data;    /*1. 打开文件*/    file=fopen("D:/123.txt","rb"); //malloc    if(file==NULL)    {        printf("文件打开失败!\n");        return -1;    }    /*2. 偏移文件指针*/    fseek(file,5,SEEK_SET);    /*3. 读数据---验证文件指针是否可否自动向后偏移*/    cnt=fread(&data,1,1,file);    printf("data=%c\n",data);    /*4. 关闭文件*/    fclose(file);  //free     return 0;}
2.7 以上午所学的函数,如何判断文件读完了?到文件结尾?
#include <stdio.h>#include <stdlib.h>#include <string.h>int main(){    FILE *file;    int cnt;    char data;    /*1. 打开文件*/    file=fopen("D:/123.txt","rb"); //malloc    if(file==NULL)    {        printf("文件打开失败!\n");        return -1;    }    /*2. 偏移文件指针*/    fseek(file,5,SEEK_SET);    /*3. 读数据---验证文件指针是否可否自动向后偏移*/    while(1)    {        cnt=fread(&data,1,1,file);        if(cnt!=1)break;        printf("data=%c\n",data);    }    /*4. 关闭文件*/    fclose(file);  //free     return 0;}
2.8 文件读写结构体数据
//写结构体数据#include <stdio.h>#include <stdlib.h>#include <string.h>struct MyStruct{    int a;    int b;    char c[100];};int main(){    FILE *file;    int cnt;    struct MyStruct stu={666,888,"C语言文件操作学习"};    /*1. 打开文件*/    file=fopen("D:/123.txt","wb");     if(file==NULL)    {        printf("文件打开失败!\n");        return -1;    }    /*2. 读数据*/    cnt=fwrite(&stu,1,sizeof(struct MyStruct),file);    printf("cnt=%d\n",cnt);    /*3. 关闭文件*/    fclose(file);  //free     return 0;}//读结构体数据#include <stdio.h>#include <stdlib.h>#include <string.h>struct MyStruct{    int a;    int b;    char c[100];};int main(){    FILE *file;    int cnt;    struct MyStruct stu;    /*1. 打开文件*/    file=fopen("D:/123.txt","rb");     if(file==NULL)    {        printf("文件打开失败!\n");        return -1;    }    /*2. 读数据*/    cnt=fread(&stu,1,sizeof(struct MyStruct),file);    printf("cnt=%d\n",cnt);    printf("%d,%d,%s\n",stu.a,stu.b,stu.c);    /*3. 关闭文件*/    fclose(file);  //free     return 0;}
2.9 文件操作的作业练习
1.  学习文件基本读写使用2.  编写文件拷贝程序。 实现文件拷贝。3.  文件加密解密实现。 需要编写一个菜单。4.   完善学生管理系统。需要将所有学生信息保存到文件里,完善功能。

标签: #c语言中怎么调试方法 #c语言怎么保存文件 #c语言后面加0 #如何用c语言保存用户的信息数据文件 #c语言操作鼠标函数