IO day21

1、获取系统时间写入文件

#include
#include
#include
#include

int main(int argc, const char *argv[])
{
	FILE *fp = NULL;
	int count = 1;
	char str1[50];
	char str2[50];
	if((fp=fopen("./work1.txt","a")) == NULL)
	{
		perror("open file");
		return -1;
	}
	while(1)
	{
		time_t sys_time = time(NULL);
		//将毫秒数转变成时间结构体
		struct tm *fomattime = localtime(&sys_time);
		sprintf(str1,"%4d-%2d-%2d %2d:%2d:%2d\n",fomattime->tm_year+1900,\
			fomattime->tm_mon+1,\
			fomattime->tm_mday,\
			fomattime->tm_hour,\
			fomattime->tm_min,\
			fomattime->tm_sec);
		if(strcmp(str1,str2) != 0)
		{	
			fprintf(fp,"[%d]:str1 = %s\n",count,str1);
			strcpy(str2,str1);
			count++;
		}
	}
	fclose(fp);
	return 0;
}

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

#include
#include
#include
int main(int argc, const char *argv[])
{
	FILE *fpr,*fpw;
	if((fpr=fopen("./test.txt","r"))==NULL)
	{
		perror("open file");
		return -1;
	}
	if((fpw=fopen("./work2.txt","w"))==NULL)
	{
		perror("open file");
		return -1;
	}
	int ret;
	char str[100]={0};
	while((ret = fread(str,sizeof(char),1,fpr) !=0))
	{
		fwrite(str,sizeof(char),1,fpw);
	}
 	
	return 0;
}

 IO day21_第1张图片

 IO day21_第2张图片

3、思维导图

 

你可能感兴趣的:(开发语言,linux)