IO进程线程(IO函数)
1、思维导图
2、
#include <head.h>
int main(int argc, const char *argv[])
{
//打开
FILE* fp=fopen("./time.txt","a+");
if(fp==NULL)
{
PRINT_ERROR("fopen");
}
int serial=0;
char buf[128];
while(fgets(buf,sizeof(buf),fp)!=0)
{
serial++;
}
//printf("%d\n",serial);
//时间获取
//序号
fclose(fp);
while(1)
{
FILE* fp=fopen("./time.txt","a+");
time_t t=time(NULL);
struct tm *now=localtime(&t);
serial++;
if(now==NULL)
PRINT_ERROR("localtime error");
fprintf(fp,"%d %d-%d-%d %02d:%02d:%02d\n",serial,now->tm_year+1900,now->tm_mon+1,now->tm_mday,now->tm_hour,now->tm_min,now->tm_sec);
printf("%d %d-%d-%d %02d:%02d:%02d\n",serial,now->tm_year+1900,now->tm_mon+1,now->tm_mday,now->tm_hour,now->tm_min,now->tm_sec);
fclose(fp);
sleep(1);
}
return 0;
}
3、
#ifndef _HEAD_H_
#define _HEAD_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define PRINT_ERROR(mesg) do{perror(mesg);return -1;}while(0)
#endif
int main(){
FILE* fp=fopen(" '2025-03-04 10-13-26 的屏幕截图.png' ","r");
if(fp==NULL)
{
PRINT_ERROR("fopen");
}
FILE* fp1=fopen(" '2025-03-04 20-47-52 的屏幕截图.png' ","w+");
if(fp1==NULL)
{
PRINT_ERROR("fopen");
}
char buf[]={0};
while(fread(buf,sizeof(buf),1,fp)!=0)
{
if(fwrite(buf,sizeof(buf),1,fp1)==0)
return -1;
}
fclose(fp);
fclose(fp1);
return 0;
}