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

IO作业代码

问题

通过 fwrite和 fread去拷贝 文件到另外一个文件上 

#include<myhead.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<errno.h>
#include<time.h>
int main(int argc, const char *argv[])
{
FILE *fp =fopen(argv[1],"r");
if(fp==NULL)
{
perror("-1");
return 0;

}
char arr[100]=" ";
int res=0;
FILE *op=fopen(argv[2],"w+");
if(op==NULL)
{

perror("-1");
return -1;

}
while((res=fread(arr,1,sizeof(arr),fp))>0)
{
fwrite(arr,1,res,op);


}

}

向文件中每一秒打印当前系统时间

include<myhead.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<errno.h>
#include<time.h>
typedef  struct times
{
int year;
int month;
int day;
int hour;
int min;
int sec;
int d;
} ss;
int getss(FILE*fp)
{
char b[256]=" ";
int a=0;
while(fgets(b,sizeof(b),fp)!=NULL)
{

	sscanf(b,"%d",&a);
}
return a;
}
int main(int argc, const char *argv[])
{
  time_t sys_time =0;
  int c=0;
  
   int a=-1;
   FILE *fp=fopen("suhui.txt","r");
if(fp==NULL)
{
 fp=fopen("suhui.txt","w+");


}
   else
{ c=getss(fp);
   fclose(fp);
fp=fopen("suhui.txt","w+");}
      if(fp==NULL)
   {
   perror("-1");
   }
    if(c==0)
  {c=1;

  }
  else
  {c++;
  }
while(1)
{ time(&sys_time);
  
   ss s;
	struct tm *tm_ptr=localtime(&sys_time);
s.year=tm_ptr->tm_year+1900;
s.month=tm_ptr->tm_mon+1;
s.day= tm_ptr->tm_mday;
s.hour=tm_ptr->tm_hour;
s.min=tm_ptr->tm_min;
s.sec=tm_ptr->tm_sec;
if(s.sec!=a)
{fprintf(fp," %d.0 %4d-%2d-%2d %2d:%2d:%2d\n ",c,s.year,s.month,s.day,s.hour,s.min,s.sec );
fflush(fp);
c++;

}

a=s.sec;
}
fclose(fp);
return 0;

}





















3. fgets去计算\n

#include<myhead.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<errno.h>
#include<time.h>
int main(int argc, const char *argv[])
{
FILE *fp =fopen(argv[1],"r");
if(fp==NULL)
{
perror("-1");
return 0;

}
char arr[100]=" ";
int res=0;
FILE *op=fopen(argv[2],"w+");
if(op==NULL)
{

perror("-1");
return -1;

}
while((res=fread(arr,1,sizeof(arr),fp))>0)
{
fwrite(arr,1,res,op);


}
fclose(fp);
fclose(op);
fp=fopen(argv[2],"r");
char b[20]=" ";

int h=0;
while(1)
{

char *t=fgets(b,sizeof(b),fp);
if(t==NULL)
{break;}
if(b[strlen(b)-1]=='\n')
{
h++;

}


}



printf("%d",h);






}


http://www.kler.cn/a/349512.html

相关文章:

  • Java 根据指定字段实现对对象进行去重
  • Study:day11-数据可视化之Matplotlib模块
  • Python 工具库每日推荐 【sqlparse】
  • 读取 json 文件
  • 工资保证金监管平台有多重要?
  • 2024.10月12日--- SpringMVC异常处理
  • Spring Boot集成Spring Security之自动装配
  • Flink 04 | 窗口介绍 - 无界数据流的核心
  • MediaGo:革新视频下载体验的开源神器
  • 安卓上的iso 是哪几个gain 相乘
  • 软件开发----SQL基础每日刷题(转载于牛客)
  • 前端入门学习之css盒子原则
  • 基于K-means和RFM模型的电商行业用户画像及商品个性化推荐研究
  • 【华为】基于华为交换机的VLAN配置与不同VLAN间通信实现
  • QT--QPushButton设置文本和图标、使能禁能、信号演示
  • MySQL-02.概述-安装配置
  • 力扣hot100--二叉树
  • 大模型从入门到应用——LangChain:模型(Models)-[大型语言模型(LLMs):基础知识!
  • IO进程---day2
  • 集师知识付费小程序:打造培训机构在线教育的金字招牌 集师知识付费系统 集师知识付费小程序 集师知识服务系统 集师线上培训系统 集师线上卖课小程序