1
#include<head.h>
int count(FILE *fp)
{
char buf[128]="";
int sum=0;
while((fgets(buf,sizeof(buf),fp)) != NULL)
{
if(buf[strlen(buf)-1] == '\n')
{
sum++;
}
}
return sum;
}
int main(int argc, char const *argv[])
{
FILE *fp = fopen("./1.txt","a+");
if (fp == NULL) {
perror("导出失败\n");
return -1;
}
int line = count(fp);
while(1){
time_t systime = time(NULL);
time_t oldtime;
struct tm *times = localtime(&systime);
if(systime != oldtime){//当新时间变化 也就是秒数+1后执行
oldtime = systime;//重置旧时间
fprintf(fp,"%3d、%4d-%2d-%2d,%2d:%2d:%2d\n",++line,times->tm_year+1900,times->tm_mon+1,times->tm_mday,times->tm_hour,times->tm_min,times->tm_sec);
//刷新缓冲区
fflush(fp);
}
}
fclose(fp);
return 0;
}
2
#include <head.h>
int main(int argc, char const *argv[])
{
FILE *fp=NULL;
if((fp=fopen("./usr.txt","w"))==NULL)
{
perror("fopen error");
return -1;
}
//准备字符串写入文件
char buf[]="hello a\n";
fwrite(buf,1,strlen(buf),fp);
fclose(fp);
if((fp=fopen("./usr.txt","r"))==NULL)//以只读的形式打开usr
{
perror("fopen error");
return -1;
}
FILE *fq=NULL;
if((fq=fopen("./usr1.txt","w"))==NULL)//以只写的形式打开usr1
{
perror("fopen error");
return -1;
}
while(1)
{
char rbuf[2]="";//举例 当储存数组比储存的字符串短
int res=fread(rbuf,1,sizeof(rbuf),fp);//循环读取fp
if(res==0)//当读取的字符串数量为0 即到结尾
{
return -1;
}
fwrite(rbuf,1,res,fq);//写入fq
}
fclose(fp);
fclose(fq);
}
3
#include <head.h>
int main(int argc, char const *argv[])
{
FILE *fp=NULL;
if((fp=fopen("./gg.bmp","r+"))==NULL)
{
perror("fopen error");
return -1;
}
int img_size=0;
fseek(fp,2,SEEK_SET);
fread(&img_size,sizeof(img_size),1,fp);
printf("size=%d\n",img_size);
fseek(fp,54,SEEK_SET);
for(int i=0;i<960/2;i+50)
{
unsigned char color[3];
fread(color,sizeof(color),1,fp);//读取左下角的颜色块
for(int j=0;j<50;j++)
{
fwrite(color,sizeof(color),1,fp);//将后面50个颜色块的颜色都变为第一个
}
}
fclose(fp);
return 0;
}