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

Linux:libc库简单设计

简单设计MyFopen,MyFclose,MyFwrite,MyFFlush

mystdio.h

#pragma once
#include<stdio.h>#define MAX 1024
#define NONE_FLUSH 1<<0
#define LINE_FLUSH 1<<1
#define FULL_FLUSH 1<<2typedef struct IO_FILE
{int fileno;int flag;char outbuffer[MAX];int bufferlen;int flush_method;
}MyFile;MyFile* MyFopen(const char* path, const char* mode);
void MyFclose(MyFile*);
int MyFwrite(MyFile*, void* str, int len);
void MyFFlush(MyFile*);

mystdio.c

#include"mystdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>static MyFile* BuyFile(int fd, int flag)
{MyFile* f = (MyFile*)malloc(sizeof(MyFile));if (f == NULL) return NULL;f->fileno = fd;f->flag = flag;f->bufferlen = 0;f->flush_method = LINE_FLUSH;memset(f->outbuffer, 0, sizeof(f->outbuffer));return f;
};MyFile* MyFopen(const char* path, const char* mode)
{int fd = -1;int flag = 0;if (strcmp(mode, "w") == 0){flag = O_CREAT | O_WRONLY | O_TRUNC;fd = open(path, flag, 0666);}else if (strcmp(mode, "a") == 0){flag = O_CREAT | O_WRONLY | O_APPEND;fd = open(path, flag, 0666);}else if (strcmp(mode, "r") == 0){flag = O_RDWR;fd = open(path, flag);}else{}if (fd < 0)return NULL;return BuyFile(fd, flag);
}void MyFclose(MyFile* file)
{if (file->fileno < 0) return;MyFFlush(file);close(file->fileno);free(file);
}int MyFwrite(MyFile* file, void* str, int len)
{memcpy(file->outbuffer + file->bufferlen, str, len);file->bufferlen += len;if (file->flush_method & LINE_FLUSH && file->outbuffer[file->bufferlen - 1] == '\n'){MyFFlush(file);}return 0;
}void MyFFlush(MyFile* file)
{if (file->bufferlen <= 0) return;int n = write(file->fileno, file->outbuffer, file->bufferlen);(void)n;fsync(file->fileno);file->bufferlen = 0;
}

usercode.c

#include "mystdio.h"
#include <string.h>
#include <unistd.h>int main()
{MyFile* filep = MyFopen("./log.txt", "a");if (!filep){printf("fopen error!\n");return 1;}int cnt = 10;while (cnt--){char* msg = (char*)"hello myfile!!!";MyFwrite(filep, msg, strlen(msg));MyFFlush(filep);printf("buffer: %s\n", filep->outbuffer);sleep(1);}MyFclose(filep); // FILE *fpreturn 0;
}

http://www.xdnf.cn/news/334603.html

相关文章:

  • Java响应实体【R】
  • JavaScript 性能优化全攻略:从基础到实战
  • PDF生成模块开发经验分享
  • element MessageBox 实现底部三个按钮或者更多按钮—开箱即用
  • Spring Cloud:概述,服务注册和服务发现,多机部署和负载均衡
  • 二本计算机,毕业=失业?
  • 【Rust】结构体
  • 【算法学习】递归、搜索与回溯算法(二)
  • 计算机网络:深入分析三层交换机硬件转发表生成过程
  • 为了摸鱼和吃瓜,我开发了一个网站
  • 酒店客房拖鞋材质款式多样,对顾客入住感受影响大
  • 面试实践AND面经热点题目总结
  • 紫禁城多语言海外投资理财返利源码带前端uniapp纯工程文件
  • C++ Primer (第五版)-第十四章重载运算与类型转换
  • 雷军「去执行化」与小米汽车更名:一场关乎安全与战略的双向奔赴|创客匠人热点评述
  • 软件工程之需求分析涉及的图与工具
  • V 型球阀:多材质多驱动,精准适配复杂严苛工况-耀圣
  • 开源照片管理系统PhotoPrism的容器化部署与远程管理配置
  • 【electron+vue】常见功能之——调用打开/关闭系统软键盘,解决打包后键盘无法关闭问题
  • Inno Setup专业打包指南:从基础到高级应用
  • 没有Mac,我是怎么上传IPA到App Store的?
  • maven如何搭建自己的私服(LINUX版)?
  • 【Linux修炼手册】Linux开发工具的使用(一):yum与vim
  • 网易游戏 Flink 云原生实践
  • OrangePi Zero 3学习笔记(Android篇)3 - 串口
  • Qt实现车载多媒体项目,包含天气、音乐、视频、地图、五子棋功能模块,免费下载源文件!
  • Linux/AndroidOS中进程间的通信线程间的同步 - 消息队列
  • nputop:交互式 Ascend NPU 进程查看器(nvitop昇腾版)
  • 视觉图像处理及多模态融合初探
  • MyBatis(进阶)(xml标签)