#include <myhead.h>
int fp_count(FILE *fp)
{
char buf[128] = "";
int count = 1;
while(1)
{
if(fgets(buf, sizeof(buf), fp) == NULL)
{
break;
}
else if(buf[strlen(buf)-1] == '\n')
{
count++;
}
}
return count;
}
int main(int argc, char const *argv[])
{
FILE * fp = NULL;
if((fp = fopen("./file1.txt", "a+")) == NULL)
{
return -1;
}
time_t sys_time;
struct tm *time_ptr;
char old_buf[128] = "";
char new_buf[128] = "";
setvbuf(fp, new_buf, _IONBF, 128);
int count = fp_count(fp);
while(1)
{
sys_time = time(NULL);
time_ptr = localtime(&sys_time);
sprintf(new_buf, "%4d-%2d-%2d %2d:%2d:%2d\n",\
time_ptr->tm_year+1900,\
time_ptr->tm_mon+1,\
time_ptr->tm_mday,\
time_ptr->tm_hour,\
time_ptr->tm_min,\
time_ptr->tm_sec);
if(strcmp(old_buf, new_buf))
{
fprintf(fp, "%d--", count++);
fputs(new_buf, fp);
strcpy(old_buf, new_buf);
}
}
return 0;
}
#include <myhead.h>
int main(int argc, char const *argv[])
{
FILE *fp1 = NULL;
FILE *fp2 = NULL;
if((fp1 = fopen("./d23.c", "r")) == NULL)
{
perror("fopen error");
return -1;
}
if((fp2 = fopen("./file3.txt", "w")) == NULL)
{
perror("fopen error");
return -1;
}
char buf[1] = "";
while(1)
{
int res = fread(buf, 1, 1, fp1);
if(res <= 0)
{
break;
}
fwrite(buf, 1, 1, fp2);
}
fclose(fp1);
fclose(fp2);
return 0;
}