当前位置: 首页 > article >正文

嵌入式学习Day17 linux高级编程 -- 输入输出

   1.fputc

     格式:
       int fputc(int c, FILE *stream);

     功能:
        向流中写入一个字符

     参数:
        c:写入字符的ASCII码值
        stream:文件流指针

     返回值:
        成功返回写入字符的ASCII码值
        失败返回EOF

     注意:
        1.fputc只能写入一个字符,写入多个字符需要多次调用fputc
        2.fputc只能写入字符
 

练习:    1.利用fputc向文件file.txt中写入字符串"hello world"
               2.搜索fgetc的功能,参数,返回值含义,实现读取文件中所有的内容并显示在界面上
    

#include <stdio.h>

int main(void)
{
    FILE *fp = NULL;
    char tmpbuff[32] = {"hello world"};
    char *pstr = NULL;

    fp = fopen("file.txt", "w");
    if (NULL == fp)
    {
        perror("fail to fopen");
        return -1;
    }

    pstr = tmpbuff;
    while (*pstr != '\0')
    {
        fputc(*pstr, fp);
        pstr++;
    }

    fclose(fp);

    return 0;
}

    
    2.fgetc 

      格式:
        int fgetc(FILE *stream);
      功能:
        从流中读取下一个字符
      参数:
        stream:文件流指针
      返回值:
        成功返回读到字符的ASCII码值
        失败返回EOF
        读到文件末尾返回EOF   

    练习:

            1.用fgetc从一文件中读取一个字符
            2.编写程序统计文件的行数

//练习1
#include <stdio.h>

int main(void)
{
    FILE *fp = NULL;
    char ch = 0;

    fp = fopen("file.txt", "r");
    if (NULL == fp)
    {
        perror("fail to fopen");
        return -1;
    }
    
    while (1)
    {
        ch = fgetc(fp);  //要读取第二个字符还是用该语句,第几个都是,遵循顺序原则
        if (EOF == ch)
        {
            break;
        }
        printf("ch = %c\n", ch);
    }

    fclose(fp);

    return 0;
}
//练习2
#include <stdio.h>

int main(void)
{
    FILE *fp = NULL;
    char ch = 0;
    int cnt = 0;

    fp = fopen("file.txt", "r");
    if (NULL == fp)
    {
        perror("fail to fopen");
        return -1;
    }

    while (1)
    {
        ch = fgetc(fp);
        if (EOF == ch)
        {
            break;
        }

        if ('\n' == ch)
        {
            cnt++;
        }
    }

    fclose(fp);

    printf("cnt = %d\n", cnt);

    return 0;
}

    2.fgetc/fputc与getchar/putchar的区别(基本无区别)
        char ch = 0;
        ch = getchar();
        ch = fgetc(stdin);

        char ch = 'a'
        putchar(ch);
        fputc(ch, stdout);

    练习:实现将一个文件中的内容拷贝到另一个文件中

#include <stdio.h>

int main(void)
{
    FILE *fsrc = NULL;
    FILE *fdst = NULL;
    char tmp = 0;

    fsrc = fopen("src.txt", "r");
    if (NULL == fsrc)
    {
        perror("fail to fopen");
        return -1;
    }

    fdst = fopen("dst.txt", "w");
    if (NULL == fdst)
    {
        perror("fail to fopen");
        return -1;
    }

    while (1)
    {
        tmp = fgetc(fsrc);
        if (EOF == tmp)
        {
            break;
        }
        fputc(tmp, fdst);
    }

    fclose(fsrc);
    fclose(fdst);

    return 0;
}


    3.fputs

      格式:
        int fputs(const char *s, FILE *stream);
      功能:
        向流中写入一个字符串
      参数:
        s:字符串首地址
        stream:文件流指针
      返回值:
        成功返回非负数
        失败返回EOF
        
    练习:向一文件中写入一个字符串

#include <stdio.h>

int main(void)
{
    FILE *fp = NULL;
    char str[32] = {"hello world"};

    fp = fopen("file.txt", "w");
    if (NULL == fp)
    {
        perror("fail to fopen");
        return -1;
    }

    fputs(str, fp);

    fclose(fp);

    return 0;
}


    4.fgets

      格式:
        char *fgets(char *s, int size, FILE *stream);
      功能:
        从流中读取一个字符串
      参数:
        s:存放字符串空间首地址
        size:最多读取字符的个数(包括函数自己最后在末尾自己添加的'\0')
        stream:文件流指针
      返回值:
        成功返回存放字符串空间的首地址
        失败返回NULL
        读到文件末尾返回NULL
      结束标志:
        1.遇到'\n'读取后结束,并在结尾处增加'\0'
        2.读满size个满结束
      
练习:从一文件中获取字符串
        

#include <stdio.h>

int main(void)
{
    FILE *fp = NULL;
    char tmpbuff[3] = {0};
    
    fp = fopen("file.txt", "r");  //假设此时文件存在,里边有一串字符hello world
    if (NULL == fp)
    {
        perror("fail to fopen");
        return -1;
    }

    fgets(tmpbuff, sizeof(tmpbuff), fp);   //因为sizeof()结果是3,且函数自带在文件末尾输入'\0'
    printf("tmpbuff = %s\n", tmpbuff);     //所以打印结果是he

    fclose(fp);

    return 0;
}

size_t //无符号的long型


    5.fwrite 

      格式:
      size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
      功能:
        向流中写入nmemb个对象,每个对象size字节大小,在ptr指向的空间中
      参数:
        ptr:存放数据空间的首地址
        size:每个数据对象的大小
        nmemb:数据对象的个数
        stream:文件流指针
      返回值:
        成功返回写入对象的个数
        失败返回0
        读到文件末尾返回0 

练习:使用fwrite向文件中写入一个、多个对象
      

#include<stdio.h>

typedef struct student
{
    char name[20];
    char sex;
    int age;
    int score;
}stu_t;

int main()
{
    FILE *fp = NULL;
//    stu_t a = {"zhangsan",'m',13,66};

    stu_t arr[] = {{"zhangsan",'m',13,66},{"lisi",'w',34,66},{"wangwu",'m',34,44},{"maliu",'w',23,43}};
    
    fp = fopen("file.txt","w");
    if(fp == NULL)
    {
        perror("fail open");
        return -1;
    }

//    fwrite(&a,sizeof(stu_t),1,fp);    //写入一个对象
      fwrite(&arr,sizeof(stu_t),4,fp);  //写入多个对象
                                                      //&arr取到了首元素地址,arr也是
    fclose(fp);

    return 0;
}


    6.fread 

      格式:
      size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
      功能:
        从流中读取nmemb个对象,每个对象size个字节,存放到ptr指向的空间中
      参数:
        ptr:存放读取内容空间首地址
        size:读取对象的大小
        nmemb:读取对象的个数
        stream:文件流指针 
      返回值:
        成功返回读到对象个数
        失败返回0
        读到文件末尾返回0 

      练习:
         以结构体学生作为对象,使用fread进行读取        

#include<stdio.h>

typedef struct student
{
    char name[20];
    char sex;
    int age;
    int score;

}stu_t;

#if 0
void showstu(stu_t tmpstu) //传值
{
    printf("姓名:%s\n",tmpstu.name);
    printf("性别:%c\n",tmpstu.sex);
    printf("年龄:%d\n",tmpstu.age);
    printf("分数:%d\n",tmpstu.score);
}
#endif

void showstu(stu_t* tmpstu) //传址
{
    printf("姓名:%s\n",tmpstu->name);
    printf("性别:%c\n",tmpstu->sex);
    printf("年龄:%d\n",tmpstu->age);
    printf("分数:%d\n",tmpstu->score);
}

int main()
{
    int i = 0;
    stu_t a;
    stu_t s[4];

    FILE *fp = NULL;

    fp = fopen("file.txt","r");
    if(fp == NULL)
    {
        perror("fail to opened!\n");
        return -1;
    }

#if 0
//读取一个对象
    fread(&a,sizeof(stu_t),1,fp);
    showstu(a);
#endif


#if 0
//读取多个对象(传值)
    fread(s,sizeof(stu_t),4,fp);

    for(i= 0;i < 4;i++)
    {
        showstu(s[i]);
    }
#endif

#if 0
//读取多个对象(传址)
    fread(s,sizeof(stu_t),4,fp);

    for(i= 0;i < 4;i++)
    {
        showstu(s+i);
    }
#endif 
        
    fclose(fp);

    return 0;
}

练习:

利用fread和fwrite完成将src.jpg图片内容拷贝到dst.jpg图片中 
        char tmpbuff[4096];

#include<stdio.h>

int main()
{
    char arr[4096] = {0};
    FILE* fsrc = NULL;
    FILE* fdest = NULL;
    int r = 0;

    fsrc = fopen("src.jpg","r");
    if(fsrc == NULL)
    {
        perror("fail to fsrc\n");
        return -1;
    }
    fdest = fopen("dest.jpg","w");
    if(fdest == NULL)
    {
        perror("fail to fdest\n");
        return -1;
    }

#if 0
    while(1)
    {
        r = fread(arr,sizeof(arr),1,fsrc); //读取1个对象,每个对象4096个字节
        if(r == 0)
        {
            break;
        }
        fwrite(arr,sizeof(arr),1,fdest);  //写1个对象,每个对象4096个字节
    }
#endif


#if 0
//确保dest.jog文件和src.jog相比一个字节不多一个字节不少
    while(1)
    {
        r = fread(arr,1,sizeof(arr),fsrc); //读取4096个对象,每个对象1个字节
        if(r == 0)
        {
            break;
        }
        fwrite(arr,1,r,fdest); //写r个对象,每个对象1个字节
    }
#endif

    fclose(fsrc);
    fclose(fdest);

    return 0;
}


    7.fprintf 

      格式:
      int fprintf(FILE *stream, const char *format, ...);
      功能:
        将格式化字符串输出到stream指向的流中
      注释:
        fprintf的用法只是在printf的基础上在""(双引号)前边添加文件流指针,使要输出的内容输出在对应文件中

printf("hello world\n");
fprintf(stdout,"hello world\n");
        
printf("%d",a);
fprintf(fp,"%d",a);

    8.fsancf 

      格式:
      int fscanf(FILE *stream, const char *format, ...);
      功能:
        从流中读取格式化的字符串
      注释:
        fscanf的用法只是在scanf的基础上在""(双引号)前边添加文件流指针,从文件中读取原本要在键盘上输入的内容
        
       练习:fscanf 具体功能的练习

#include<stdio.h>

int main()
{
    FILE* fp = NULL;
    int NUM1 = 0;
    int NUM2 = 0;

    fp = fopen("file.txt","r");
    if(fp == NULL)
    {
        perror("fail to open file.txt");
        return -1;
    }

    fscanf(fp,"NUM1 = %d NUM2 = %d",&NUM1,&NUM2); //此时fp所指的文件中的内容是NUM1 = 100 NUM2 = 200(除此以外的内容编译的时候均会报错)

    fclose(fp);

    printf("NUM1 = %d NUM2 = %d",NUM1,NUM2);

    return 0;
}


http://www.kler.cn/news/233076.html

相关文章:

  • 边缘计算中的能源效率与运维成本
  • XML介绍和基本语法
  • 深入理解Python爬虫的四大组件之Logger(记录器)
  • 用bootstrap结合jQuery实现简单的模态对话框
  • Java图形化界面编程—— LayoutManager布局管理器笔记
  • Flink cdc debug调试动态变更表结构
  • 同步復位和異步復位二者各自的優缺點
  • Android 粒子喷泉动效
  • Python进阶:迭代器生成器
  • 【数学建模】【2024年】【第40届】【MCM/ICM】【A题 七鳃鳗性别比与资源可用性】【解题思路】
  • 备战蓝桥杯---搜索(完结篇)
  • 无人机系统组装与调试,多旋翼无人机组装与调试技术详解,无人机飞控系统原理
  • 机器学习11-前馈神经网络识别手写数字1.0
  • 【OpenHarmony硬件操作】WIFI模块的操作(udp+tcp)
  • 比较Kamailio和OpenSIPS的重写contact函数
  • 华为机考入门python3--(10)牛客10-字符个数统计
  • PKI - 借助Nginx 实现Https 服务端单向认证、服务端客户端双向认证
  • 电脑通电自启动设置
  • 使用Python语言生成区块链地址
  • Android矩阵Matrix动画缩放Bitmap移动手指触点到ImageView中心位置,Kotlin
  • 力扣-137. 只出现一次的数字 II
  • 联合体知识点解析
  • 如何用Hexo搭建一个优雅的博客
  • 单片机学习笔记---DS1302时钟
  • django中实现登录
  • 微信小程序的图片色彩分析,窃取网络图片的主色调
  • Python中使用opencv-python库进行颜色检测
  • 【芯片设计- RTL 数字逻辑设计入门 7 -- 同步复位与异步复位详细介绍】
  • 使用Collections.singletonList()遇到的问题
  • Unity学习笔记之【IK反向动力学操作】