IOday2

 0

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
int js(FILE *fp)
{
	char buf[10];
	int count=0;
	while(fgets(buf,sizeof(buf),fp)!=NULL)
	{
		if(buf[strlen(buf)-1]=='\n')
		{
			count++;
		}
	}

	return count;

}
int main(int argc, const char *argv[])
{	
	//定义一个系统时间变量
	time_t sysTime;
	//定义时间结构体指针指向时间
	
	struct tm *tm_ptr;
	char buf[100];
		FILE *fp;
	if((fp=fopen("./zytextm.txt","a"))==NULL)
	{
			perror("fpoend error");
			return -1;
	}
	char h;

		int count=js(fp);
	while(1)
	{
		time(&sysTime);//通过参数j计算时间
		tm_ptr=localtime(&sysTime);//转换格式
		fprintf(fp,"%d %4d-%2d-%2d %02d:%02d:%02d\n",count,\
			tm_ptr->tm_year+1900,\
			tm_ptr->tm_mon+1,\
			tm_ptr->tm_mday,\
			tm_ptr->tm_hour,\
			tm_ptr->tm_min,\
			tm_ptr->tm_sec);
		count++;
		fflush(fp);
		sleep(1);
	}

	
	fclose(fp);

	return 0;
}

linux@linux:~/IO/02$ gcc zytime.c 
linux@linux:~/IO/02$ ./a.out
^Z
[50]+  已停止               ./a.out
linux@linux:~/IO/02$ cat zytextm.txt 
1 2023- 8-16 20:59:05
2 2023- 8-16 20:59:06
3 2023- 8-16 20:59:07
4 2023- 8-16 20:59:08

2> 使用fread、fwrite完成两个文件的拷贝

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, const char *argv[])
{		//定义文件指针并打开文件
	FILE*srcfp;
	FILE*pp;
	if(argc!=3)
	{
		printf("input file error\n");
		printf("usage:./a.out srcfile dstfile\n");
		return -1;
	}
	//以读的形式打开文件
	if((srcfp=fopen(argv[1],"r"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
	if((pp=fopen(argv[2],"w"))==NULL)
	{
		perror("fopen error");
		return -1;
	}
 
	//从文件读取数据
	char buf[100];
	int num;
	while((num=fread(buf,sizeof(char),1,srcfp)!=0))
	{ 
		fwrite(buf,sizeof(char),1,pp);
	}
 
	//关闭文件
	fclose(srcfp);
	fclose(pp);
	printf("拷贝成功\n");
	return 0;
}
linux@linux:~/IO/02$ cat 02test.txt 
linux@linux:~/IO/02$ cat 01test.txt 
李白唐
唐
李白
静夜思退出
linux@linux:~/IO/02$ gcc zykb.c
linux@linux:~/IO/02$ ./a.out 01test.txt  02test.txt 
拷贝成功
linux@linux:~/IO/02$ cat 02test.txt 
李白唐
唐
李白
静夜思退出

思维导图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值