1> 写一个日志文件,将程序启动后,每一秒的时间写入到文件中
#include<myhead.h>
int main(int argc, const char *argv[])
{
time_t t = time(NULL);
struct tm *p = localtime(&t);
FILE *fp = NULL;
if((fp=fopen("./log","a"))==NULL)
{
perror("fopen error");
return -1;
}
int flag = 0;
fprintf(fp,"%4d-%2d-%2d %2d:%2d:%2d\n",p->tm_year+1900,p->tm_mon+1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
fflush(fp);
flag = p->tm_sec;
while(1)
{
time_t t = time(NULL);
struct tm *p = localtime(&t);
if(p->tm_sec!=flag)
{
fprintf(fp,"%4d-%2d-%2d %2d:%2d:%2d\n",p->tm_year+1900,p->tm_mon+1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);
fflush(fp);
flag = p->tm_sec;
}
}
return 0;
}
2> 使用fread、fwrite完成两个文件的拷贝
不允许只读写一次
#include<myhead.h>
int main(int argc, const char *argv[])
{
//定义文件指针
FILE *sfp = NULL;
FILE *dfp = NULL;
if((sfp = fopen("./hmk2.c", "r")) == NULL)
{
perror("fopen error");
return -1;
}
if((dfp = fopen("./temp.txt", "w")) == NULL)
{
perror("fopen error");
return -1;
}
char num[2] = "";
while(fread(num,1,1,sfp)==1)
{
fwrite(num,1,1,dfp);
}
//关闭文件
fclose(sfp);
fclose(dfp);
return 0;
}
3> 实现对bmp图像的读写操作
#include<myhead.h>
int main(int argc, const char *argv[])
{
FILE *fp = NULL;
if((fp = fopen("./gg.bmp", "r+")) == NULL)
{
perror("fopen error");
return -1;
}
fseek(fp, 54, SEEK_SET);
unsigned char color[3] = {0, 0, 255};
for(int i=0; i<960/2; i++)
{
for(int j=0;j<1280; j++)
{
fwrite(color, sizeof(color), 1, fp);
}
}
fclose(fp);
return 0;
}