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