自写有问题的代码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;
}