c语言-文件io

#include "stdio.h"
#include "stdlib.h"
#include "string.h"


int main()
{
    FILE *p = fopen("D:\\study\\a.txt", "w");
    fputs("hello world", p);
    fputs("hello world", p);
    fclose(p);
    return 0;
}

判断文件是否成功打开的常用方法

if((fp=fopen("D:\\study\\a.txt","w"))==NULL)
{
  printf("cant open the file");
  exit(0);
}

copy一个二进制文件;(fread(),fwrite())

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

int main()
{
    char buf[4096];
    FILE *p1 = fopen("D:\\study\\a.zip", "rb");
    FILE *p2 = fopen("D:\\study\\b.zip", "wb");
    while (!feof(p1))
    {
        memset(buf, 0, sizeof(buf));
        size_t res=fread(buf, sizeof(char), sizeof(buf), p1);
        fwrite(buf, sizeof(char), res, p2);
    }
    fclose(p1);
    fclose(p2);
    return 0;
}

获取文件基本信息(stat() )

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <sys/stat.h>

int main()
{
    struct stat buf;//需要定义结构体 struct stat buf
    stat("D:\\study\\a.zip", &buf);
    printf("file size =%d\n", buf.st_size);//输出文件大小
    return 0;
}

统计程序消耗时间(clock())

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"


int main()
{
    clock_t start_t = clock();
    代码块
    clock_t end_t = clock();
    printf("%d\n",(end_t - start_t));
    return 0;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值