IO 向文件中输出时间 fread fwrite 实现两个文件的拷贝 课堂代码

【一、向文件中输出时间】

1.向文件中输出时间

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int Line();

int main(int argc, const char *argv[])
{
    FILE *fp;
    if((fp = fopen("./time_test.txt","a+")) == NULL)
    {
        perror("fopen file error");
        return -1;
    }

    //时间函数
    time_t sys_time;
    time(&sys_time);
    struct tm *t=localtime(&sys_time);

    //行号
    int line=Line(fp);
    while(1)
    {
        
        int sec=t->tm_sec;
        
        time(&sys_time);
        t=localtime(&sys_time);
        int now=t->tm_sec;

        if(now - sec == 1)
        {
            line++;
            printf("[%d] %4d-%2d-%2d %2d-%2d-%2d\n",line,t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
            fprintf(fp,"[%d] %4d-%2d-%2d %2d-%2d-%2d\n",line,t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
            fflush(fp);
        }
    }

    return 0;
}

//自定义行号函数 
//参数:fp
//返回:行数
int Line(FILE *fp)
{
    int count=0;
    char s[100];
    rewind(fp);
    while(fgets(s,sizeof(s),fp) != NULL)
    {
        if(s[strlen(s)-1] == '\n')
            count++;
    }
    return count;
}

2.效果图


 

 【二、fread fwrite 实现文件拷贝】

1.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    FILE *dest;
    if((dest = fopen("./dest.txt","w")) == NULL)
    {
        perror("fopen file error");
        return -1;
    }

    FILE *src;
    if((src = fopen("./src.txt","r+")) == NULL)
    {
        perror("fopen file error");
        return -1;
    }

    char s[20]="hello world\n";
    fwrite(s,sizeof(s),1,src);

    rewind(src);

    char temp[20];
    fread(temp,sizeof(temp),1,src);
    fwrite(temp,sizeof(temp),1,dest);
    


    
    return 0;
}
 

2.效果图

 

【三、课堂代码】

1.fopen fclose perror

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, const char *argv[])
{
    FILE *fp = NULL;

if((fp = fopen("./01test.txt","w+")) == NULL)
{
    perror("fopen error");
}


    fputc('h',fp);
    fputc('e',fp);
    fputc('l',fp);
    fputc('l',fp);
    fputc('o',fp);
    fputc(10,fp);
    

    char ch;
    while((ch = fgetc(fp)) != EOF)
    {
        printf("%c",ch);
    }
    if(fclose(fp) != 0)
    {

        printf("关闭失败\n");
    }

    

    return 0;
}
 

.2.fgetc/fputc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
/*
    FILE *fp=NULL;
    if((fp=fopen("./02file.txt","a+")) == NULL)
    {
        perror("");
        return -1;
    }

    fputc('h',fp);
    fputc('h',fp);
    fputc('h',fp);
    fputc('h',fp);
    fputc(10,fp);
    fputc('h',fp);
    fputc('h',fp);
    fputc('h',fp);
    fclose(fp);

    if((fp=fopen("./02file.txt","a+")) == NULL)
    {
        perror("");
        return -1;
    }
    int count=0;
    char ch;
    while((ch=fgetc(fp)) != EOF)
    {
        if(ch == '\n')
            count++;
    }
    fclose(fp);
    printf("%d\n",count);
*/
    FILE *dest,*src;
    if((dest=fopen("argv[1]","w")) == NULL)
    {
        perror("");
        return -1;
    }

    if((src=fopen("argv[2]","r")) == NULL)
    {
        perror("");
        return -1;
    }

    int count;
    char ch;
    while(ch = fgetc(src) != EOF)
    {
        if(ch == '\n')
            count++;
    }

    printf("总共有%d行\n");

    return 0;
}
 

3.fgets

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    char s[10];
    fgets(s,sizeof(s),stdin);
    s[strlen(s)-1]='\0';
    printf("s = %s\n",s);
    return 0;
}
 

4.验证行缓存 全缓存 大小

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    //验证行缓存大小是否为1024字节
    printf("没有使用缓存区时: %ld\n",stdin->_IO_buf_end-stdin->_IO_buf_base);

    char s[20];
    fgets(s,sizeof(s),stdin);

    printf("s = %s\n",s);
    printf("行缓存使用缓存区时: %ld\n",stdin->_IO_buf_end-stdin->_IO_buf_base);

    //验证全缓存是否为4096字节
    FILE *fp;
    if((fp = fopen("./01test.txt","a")) == NULL)
    {
        perror("");
        return -1;
    }

    strcpy(s,"hello!");
    fwrite(s,sizeof(s),1,fp);
    printf("全缓存使用缓存区时: %ld\n",fp->_IO_buf_end-fp->_IO_buf_base);

    return 0;
}
 

5.缓存刷新时机

行缓存:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
/*
    //当缓存区没有到刷新时机时,不会刷新缓存区
    printf("16143131");
    while(2);

    //1.遇到换行符时,自动刷新缓存区
        printf("16143131\n");

    //2.程序结束时,自动刷新缓存区
    

    //3.当关闭文件时,会刷新缓存区
    printf("hello world");
    fclose(stdout);
    while(1);

    //4.当使用fflush函数刷新时,也会刷新缓存区
    printf("hello world");
    fflush(stdout);
    while(1);
*/
    //5.缓存区满了,自动刷新缓存区
    for(int i=0;i<1500;i++)
    {
        printf("A");

    }
    printf("%d\n",count);

/*
    //6.当输入输出发生切换时,也会刷新缓存区
    printf("hello world");
    int a;
    scanf("%d",&a);
    while(1);
*/
    return 0;
}
 

全缓存:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    FILE *fp;
    if((fp = fopen("./01test.txt","w")) == NULL)
    {
        perror("");
        return -1;
    }

    //1.当遇到换行符,不会刷新全缓存
    fputs("hello world\n",fp);
    while(1);

    //2.当程序结束后,会刷新全缓存

    //3.当关闭文件指针时,会刷新全缓存

    //4.当时候fflush刷新时,会刷新全缓存
    /*    
        fputs("hello world\n",fp);
        fflush(fp);
        while(1);

    //5.当全缓存满了,会自动刷新全缓存
    for(int i=0;i<5000;i++)
    {
    fputs("A",fp);
    }
    */
    //6.当输入输出切换时,会刷新全缓存'

    return 0;
}
 

6.系统时间函数

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, const char *argv[])
{
    //有关系统时间的函数 time
    time_t sys_time;
    sys_time=time(NULL);
    
    struct tm *T=localtime(&sys_time);
    
    //输出
/*    printf("%4d-%02d-%02d %02d:%02d:%02d\n",\
    T->tm_year+1900,T->tm_mon+1,T->tm_mday,\
    T->tm_hour,T->tm_min,T->tm_sec);
*/
    
    char buf[250];
    sprintf(buf,"%4d-%02d-%02d %02d:%02d:%02d\n",\
    T->tm_year+1900,T->tm_mon+1,T->tm_mday,\
    T->tm_hour,T->tm_min,T->tm_sec);
    
    printf("%s",buf);
    return 0;
}
 

7.fprintf/fscanf

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
    FILE *fp;
    char userName[100];
    char pwd[20];
    if((fp = fopen("./002test.txt","a")) == NULL)
    {
        perror("");
        return -1;
    }
    //将账号密码输入
    printf("请输入注册账号: ");
    scanf("%s",userName);
    printf("请输入密码: ");
    scanf("%s",pwd);

    //将内容写入文件中
    fprintf(fp,"%s %s\n",userName,pwd);

    //关闭
    fclose(fp);
    printf("注册成功!\n");

    if((fp = fopen("./002test.txt","r")) == NULL)
    {
        perror("");
        return -1;
    }
    
    //定义变量接受
    char login[100],password[20];
    printf("请输入注册账号: ");
    scanf("%s",login);
    printf("请输入密码: ");
    scanf("%s",password);

    while(fscanf(fp,"%s %s",userName,pwd) != EOF)
    {
        if(strcmp(userName,login) == 0 && strcmp(pwd,password) == 0)
        {
            printf("登陆成功!\n");
            fclose(fp);
            return 0;
        }
    }

    printf("登录失败!\n");
    fclose(fp);
    return 0;
}
 

8.fwrite/fread

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct stu
{
    char name[20];
    int age;
    double score;
};
int main(int argc, const char *argv[])
{

    FILE *fp;

    if((fp = fopen("./fread_write.txt","w")) == NULL)
    {
        perror("");
        return -1;
    }
/*
    char s[]="hello world\n";
    fwrite(s,sizeof(s),1,fp);
    fclose(fp);

    if((fp = fopen("./fread_write.txt","r")) == NULL)
    {
        perror("");
        return -1;
    }

    char buf1[1024]="";
int red=fread(buf1,1,sizeof(buf1),fp);

//    printf("buf1 = %s",buf1);

fwrite(buf1,1,red,stdout);
*/

    
    struct stu s[2]={{"lily",18,92},{"nink",19,88}};
        fwrite(s,sizeof(s),2,fp);
    fclose(fp);
    if((fp = fopen("./fread_write.txt","r")) == NULL)
    {
        perror("");
        return -1;
    }

    struct stu ss;
    fread(&ss,sizeof(struct stu),1,fp);
printf("%s %d %lf",ss.name,ss.age,ss.score);

    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值