当前位置:网站首页>IO进程线程->文件IO->day2
IO进程线程->文件IO->day2
2022-07-30 05:46:00 【鲸叫我照顾海QAQ】
目录
一、创建存储文件time.txt
1.1 要求创建一个time.txt,存储内容格式如下:
[1] 2022-07-28 17:15:06
[2] 2022-07-28 17:15:07
[3] 2022-07-28 17:15:08
ctrl + c退出程序,过一会儿之后重新启动程序
----------------------------------------------
[1] 2022-07-28 17:15:06
[2] 2022-07-28 17:15:07
[3] 2022-07-28 17:15:08
[4] 2022-07-28 17:16:31 <-------------------
[5] 2022-07-28 17:16:32
2.2 函数代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE *a1=fopen("./time.txt","r+");
if(a1 == NULL)
{
perror("open");
return -1;
}
long flag=-1;
while(1)
{
time_t t=time(NULL);
struct tm *t_day=localtime(&t);
while(1)
{
char arr[3]={0};
int i=0;
char *p=fgets(arr,sizeof(arr)-1,a1);
while(arr[i] != '\0')
{
if(arr[i] == '\n'){
flag++;
}
i++;
}
if(NULL == p)
{
break;
}
}
fprintf(a1,"[%ld] %d-%02d-%02d %02d:%02d:%02d\n",\
flag,t_day->tm_year+1900,t_day->tm_mon,t_day->tm_mday,t_day->tm_hour,t_day->tm_min,t_day->tm_sec);
fflush(a1);
printf("存入成功\n");
flag++;
sleep(1);
}
fclose(a1);
return 0;
}
2.3 执行结果

二、要求文件IO拷贝一张图片; eog 1.png
2.1 函数代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int fd_r=open("/home/ubuntu/图片/1.png",O_RDONLY);
int fd_w=open("./cpy.png", O_WRONLY|O_CREAT|O_TRUNC, 0664);
if(fd_r <0 && fd_w<0)
{
perror("open");
return -1;
}
char buf[10];
ssize_t res=0;
while(1)
{
bzero(buf,sizeof(buf));
res=read(fd_r,buf,sizeof(buf));
if(0 == res)
{
break;
}
write(fd_w,buf,res);
}
close(fd_r);
close(fd_w);
printf("拷贝成功\n");
return 0;
}
2.2 执行结果

边栏推荐
- 动态规划入门 JS
- Three working modes of CPU: real mode, protected mode, long mode
- 工程师必看:常见的PCB检测方法有哪些?
- -----博客声明
- VSCode hides the left activity bar
- xxx is not in the sudoers file.This incident will be reported错误
- Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
- c语言编程练习
- Explore the efficiency of make_shared
- Machine Learning, Deep Learning Based on MATLAB 2021b
猜你喜欢

干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案

【江科大自化协stm32F103c8t6】笔记之【入门32单片机及EXTI外部中断初始化参数配置】

ES6语法笔记(ES6~ES11)

【正点原子】sys.c、sys.h位带操作的简单应用

vscode 设置 sublime 的主题

【江科大自化协stm32F103c8t6】笔记之【入门32单片机及利用TIM输出比较配置PWM】

BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?

IEEE在指定期刊下搜索相关论文

js advanced study notes (detailed)

【江科大自化协stm32F103c8t6】笔记之【入门32单片机及TIM定时中断初始化参数配置】
随机推荐
三种内核结构---宏内核、微内核、混合内核
你不知道的JS思考题
C语言,库函数中qsort的用法,及解释
NS3报错 fatal error: ns3/opengym-module.h: No such file or directory
"R Language + Remote Sensing" Comprehensive Evaluation Method of Water Environment
干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案
------实现二叉搜索树BST
Sklearn : train_test_split()函数的用法
删除当前路径下含某个关键字的所有文件
VsCode连接远程服务器并修改文件代码
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
迷宫问题----经典回溯法解决
vscode set sublime theme
QT serialization 1: readyRead() function, the solution to incomplete data subcontracting
Acwing刷题第一节
ES6语法笔记(ES6~ES11)
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及EXTI外部中断初始化参数配置】
pdf和word等文档添加水印
FPGA parsing B code----serial 2
Kunlun state screen production (serial 3) - based article (button serial port to send)