xm C语言12

1.rewind函数

因为我们定义的FILE*fp 是一个指针,所以他不断移动的时候最终会到达文件末尾,我们如果还想使用这个文件关闭再打开未免太过麻烦,所以就有了rewind

rewind的作用就是将指针移动到文件开头,在 再次读取相同的文件 以及读写之间转换 时起作用

objectivec 复制代码
   #inclde<stdio.h>
   int main(){
//rewind函数
    FILE*fp=fopen("ok.txt","r");
    if(file==NULL){
    printf("error");
    return 0;}
    char x;
    while(fscanf(fp,"%c",&x)!=EOF){
        printf("%c",x);
    }
    rewind(fp);
    printf("again\n");
    while(fscanf(fp,"%c",&x)!=EOF){
        printf("%c",x);
    }
    fclose(fp);
    return 0;}

2.structure结构体写入和读取文件

写入:将结构体的内容传入到文件中保存,文件可以保存大量的结构体,使用方便

fprintf(file.name,"%s/%d....",variable);

读取:从文件中读取相应的内容传到内存,供计算机短期使用

fscanf(file.name,"%s",&x address);

objectivec 复制代码
    #include<stdio.h>
    struct school{char name[20];
               int a;//age
               char grade;//ABCD
               };
    int main(){
    FILE*file=fopen("school.txt","w");
    if(file==NULL){
    printf("error");
    return 0;}
    struct school s1={amy,18,B};
    fprintf(file,"name is %s, age is %d,grade is %c",s1.name,s1.a,s1.grade);}
    fclose(file);
    file=fopen("school.txt","r");
    char k;
    while(fscanf(file,"%c",&k)!=EOF){
        printf("%c",k);
    }
    fclose(file);
    return 0;}

这里输出用到的while循环就是当我读取的内容不等于EOF(不是结尾的时候)因此最后输出的结构就是 name is amy,age is 18,grade is B

同时在这里的while循环也可以是

fscanf(file,"name is %s,age is %d,grade is %c",&s1.name,&s1.a,&s1.grade);

printf("name is %s, age is %d,grade is %c",s1.name,s1.a,s1.grade);

fscanf的格式必须和fpeintf输入的完全匹配

结构体数组:(其实和结构体无异,只是这一步我想进行自己写入信息存储到文件中)

objectivec 复制代码
#include <stdio.h>
#include <stdlib.h>
struct school{
char name[20];
int a;//age
char grade;//ABCD
};
int main()
{ FILE*file=fopen("school.txt","w");
    struct school s[2];
   for(int o=0;o<2;o++){
    // 出错点struct school s[o]; 
    scanf("%s  %d  %c",&s[o].name,&s[o].a,&s[o].grade);
   }
    for(int i=0;i<2;i++){
    fprintf(file,"name is %s, age is %d,grade is %c",s[i].name,s[i].a,s[i].grade);}
    fclose(file);
    file=fopen("school.txt","r");
    char k;
    while(fscanf(file,"%c",&k)!=EOF){
        printf("%c",k);
    }
    fclose(file);
    return 0;}

3.search 从文件中查找

第一种 使用sctmp函数 strcmp(x,y)是逐字符比较然后进行做差,通常寻找单字符时使用

objectivec 复制代码
#include <stdio.h>
#include <stdlib.h>
int main()
{  FILE*fp=fopen("test.txt","w");
    if(fp==NULL){
        printf("error");
        return 0;
    }
    char ch[20]="letter";
    char word[]="A";
    fputs(ch,fp);
    fclose(fp);
    fp=fopen("test.txt","r");
    char k[20];
    
    int count=0;
    while(fscanf(fp,"%s",k)!=EOF){
                if(strcmp(k,word)==0)
        count++;
    }
     printf("occur %d times",count);
    fclose(fp);
    return 0;
}

第二种就是直接if进行比较(只需要换while循环部分) 如果是整数就更好做了

objectivec 复制代码
 
 while(fscanf(fp,"%c",&k)!=EOF){
 if(strcmp(k,word)==0)
        count++;
    }
 while(fscanf(fp,"%s",k)!=EOF){
 if(strcmp(k,word)==0)
        count++;
    }

4.f()的格式

首先f....()都是函数,只是简化了格式,但是仍然需要输入所需的内容来获得函数结果。他们来自于函数库 stdio.h 所以使用之前记得调用函数库

fscanf(文件指针, 格式字符串, 输入项表列) eg fscanf(fp, "%d,%f", &i, &t)

fprintf(文件指针, 格式字符串, 输出表列) eg ``fprintf(fp, "%d,%f", i, t)

fgets(字符数组, 最大字符数, 文件指针),作用是读取一行内容(包括换行符),并在末尾自动添加空字符 '\0' eg fgets(str, 100, fp) 从文件读取最多 99 个字符

fputs(字符串, 文件指针),作用是将字符串写入文件,不添加额外换行符 eg fputs("Hello", fp)

(四种用法来自夸克)

5.注意NULL EOF检查 记得关闭文件

相关推荐
较劲男子汉1 小时前
CANN Runtime零拷贝传输技术源码实战 彻底打通Host与Device的数据传输壁垒
运维·服务器·数据库·cann
wypywyp1 小时前
8. ubuntu 虚拟机 linux 服务器 TCP/IP 概念辨析
linux·服务器·ubuntu
Doro再努力2 小时前
【Linux操作系统10】Makefile深度解析:从依赖推导到有效编译
android·linux·运维·服务器·编辑器·vim
senijusene2 小时前
Linux软件编程:IO编程,标准IO(1)
linux·运维·服务器
不像程序员的程序媛2 小时前
Nginx日志切分
服务器·前端·nginx
忧郁的橙子.2 小时前
02-本地部署Ollama、Python
linux·运维·服务器
北原_春希2 小时前
如何在Vue3项目中引入并使用Echarts图表
前端·javascript·echarts
尽意啊2 小时前
echarts树图动态添加子节点
前端·javascript·echarts
吃面必吃蒜2 小时前
echarts 极坐标柱状图 如何定义柱子颜色
前端·javascript·echarts
O_oStayPositive2 小时前
Vue3使用ECharts
前端·javascript·echarts