【无标题】

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值