D2 IO作业

自写有问题的代码1 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
int main(int argc, const char *argv[])
{
	//只读可追加模式打开文件
	FILE *fp;//定义文件指针,指向要打开的文件
	int i=0;
	if((fp=fopen("./time.txt","a+"))==NULL)
		{   perror("open file");
			return -1;	
		}
	
	while((fgets(c,sizeof(c),fp))!=NULL)
	{
		if(c[strlen(c)-1]=='\n')
			line++;
	}
	while(1)
	{
	char s[50];
	char str[100]="";
	char st1[100]=""; 
	char c[1111]="";
	time_t old_time=time(NULL);//取新时间毫秒数
	//time(&sys_time);//同上
	//将毫秒数转变为时间结构体
	
	struct tm*fomattime=localtime(&old_time);
	sprintf(str,"%2d",fomattime->tm_sec);
	int i=0;
	while(i<500)
	{
		i++;
	time_t new_time=time(NULL);//取旧时间毫秒数
	struct tm*newtime=localtime(&new_time);
	sprintf(st1,"%2d",newtime->tm_sec);
	}
//	printf("  %s-%s  ",str,st1);

	if(strcmp(st1,str)!=0)
		{
		count++;
		sprintf(s,"%d.%4d-%2d-%2d %2d:%2d:%2d\n"\,count,fomattime->tm_year+1900\
			,fomattime->tm_mon+1,\
			fomattime->tm_mday,\
			fomattime->tm_hour,\
			fomattime->tm_min,\
			fomattime->tm_sec);

		fputs(s,fp);
		fflush(fp);
		
		}
	}
	fclose(fp);
	return 0;
}

 作业1 转载正确代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char const *argv[])
{

    FILE *fp;
    int sec;
    struct tm *fomattime;
    char buf[100];
    int count=0;
    if ((fp = fopen("time.txt", "a+")) == NULL)
    {
        perror("open file");
        return -1;
    }
    while (fgets(buf,sizeof(buf),fp) != NULL)
    {
        if(buf[strlen(buf)-1]=='\n')
        {
            count++;
        }
    }
    while (1)
    {
        time_t sys_time = time(NULL);
        fomattime = localtime(&sys_time);
        if(sec!=fomattime->tm_sec)
        {
        sec=fomattime->tm_sec;
        count++;
        sprintf(buf,"%d.%4d-%02d-%02d %02d:%02d:%02d\n",\
                count,
        fomattime->tm_year + 1900,
        fomattime->tm_mon + 1,
        fomattime->tm_mday,
        fomattime->tm_hour,
        fomattime->tm_min,
        fomattime->tm_sec);
        fputs(buf,fp);
        fflush(fp);
        puts(buf);
        }
    }
        fclose(fp);
    return 0;
}

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
	FILE *fp,*fc;
	if((fp=fopen("./test.txt","r"))==NULL)
	{
		perror("open file");
		return -1;
	}
	if((fc=fopen("./man.txt","w"))==NULL)
	{
		perror("open file");
		return -1;
	}
	int a;
	char s1[100]="";
	while(a=fread(s1,sizeof(char),1,fp)!=0)
	{
		fwrite(s1,sizeof(char),1,fc);
	}
	//关闭文件
	fclose(fp);
	fclose(fc);
	return 0;
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值