c语言读写二进制文件

int totalcount = 0;
	//读取文件写入文件
	FILE* fp = fopen("D:\\software\\qt-opensource-windows-x86-mingw530-5.7.1.exe", "rb");
	//获取文件的大小
	if (!fp)
	{
		return -1;
	}
	FILE* fp2 = fopen("D:\\software\\qt.exe", "wb");
	//fseek(fp, 0, SEEK_END);
	//int size = ftell(fp);
	//fseek(fp, 0, SEEK_SET);
	char buf[BUFFERSIZE];
	int len = 0;
	while(!feof(fp))
	{
		len = fread(buf, 1, BUFFERSIZE, fp);
		if(len >=0)
		{
			totalcount += len;
			fwrite(buf, 1, len, fp2);
		}
		
	}
	fclose(fp);
	fclose(fp2);



// testFile.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <stdio.h>

using namespace std;
#pragma warning(disable:4996)
void WriteTXTFile_C()
{
	FILE * fp = fopen("c.txt", "wt");
	for (int i = 0; i < 1000; i++)
	{
		for (int j = 0; j < 1000; j++)
		{
			fputc('C', fp);
			fputs("Hello world!\n", fp);
		}
	}
	fclose(fp);
}

void ReadTXTFile_C()
{
	FILE* fp = fopen("c.txt","rt");
	char str[15];
	for (int i = 0; i < 1000; i++)
	{
		for (int j = 0; j < 1000; j++)
		{
			fgetc(fp);
			fgets(str, 15, fp);
		}
	}
	fclose(fp);
}

void WriteBINFile_C()
{
	FILE* fp = fopen("d.bin", "wb");
	const char* str = "CHello,world\n";
	int len = strlen(str);
	for (int i = 0; i < 1000; i++)
	{
		for (int j = 0; j < 1000; j++)
		{
			fwrite(str, 1, len, fp);
		}
	}
	fclose(fp);
}

void ReadBINFile_C()
{
	FILE* fp = fopen("d.bin", "rb");
	char str[16];
	for (int i = 0; i < 1000; i++)
	{
		for (int j = 0; j < 1000; j++)
		{
			fread(str, 1, 16, fp);
		}
	}
	fclose(fp);
}

void WriteTXTFile_CPlus()
{
	fstream file;
	file.open("cp.txt", ios::in | ios::out | ios::trunc);//注意ios::in,或者ios::in | ios::out两种方式在文件不存在时会执行写操作但不会建立文件。
	for(int i=0;i<1000;i++)
	{
		for (int j = 0; j < 1000; j++)
		{
			file.put('C');
			file << "Hello,world\n";
		}
	}
	file.close();
}

void ReadTXTFile_CPlus()
{
	fstream file;
	file.open("cp.txt",ios::in|ios::out);
	char str[15];
	for (int i = 0; i < 1000; i++)
	{
		for(int j=0;j<1000;j++)
		{
			file.get(str, 15);
		}
	}
	file.close();
}

void WriteBINFile_CPlus()
{
	fstream file;
	file.open("cp.bin", ios::in | ios::out | ios::trunc | ios::binary);
	const char* str = "CHello world!\n";
	int len = strlen(str);
	for (int i = 0; i < 1000; i++)
	{
		for (int j = 0; j < 1000; j++)
		{
			file.write(str, len);
		}
	}
	file.close();
}

void ReadBINFile_CPlus()
{
	fstream file;
	file.open("d.bin", ios::in | ios::out | ios::binary);
	char str[16];
	for (int i = 0; i < 1000; i++)
	{
		for (int j = 0; j < 1000; j++)
		{
			file.read(str, 16);
		}
	}
	file.close();
}

#define BUFFERSIZE 1024
int main()
{
	/*DWORD start, end;
	start = GetTickCount();
	WriteTXTFile_C();
	end = GetTickCount();
	printf("C语言写文本文件用时:%dms\n",end-start);
	start = GetTickCount();
	WriteTXTFile_CPlus();
	end = GetTickCount();
	printf("C++写文本文件用时:%dms\n", end - start);

	start = GetTickCount();
	WriteBINFile_C();
	end = GetTickCount();
	printf("C语言写二进制文件用时:%dms\n", end - start);
	start = GetTickCount();
	WriteBINFile_CPlus();
	end = GetTickCount();
	printf("C++写二进制文件用时:%dms\n", end - start);

	start = GetTickCount();
	ReadTXTFile_C();
	end = GetTickCount();
	printf("C语言读文本文件用时:%dms\n", end - start);
	start = GetTickCount();
	ReadTXTFile_CPlus();
	end = GetTickCount();
	printf("C++读文本文件用时:%dms\n", end - start);

	start = GetTickCount();
	ReadBINFile_C();
	end = GetTickCount();
	printf("C语言读二进制文件用时:%dms\n", end - start);
	start = GetTickCount();
	ReadBINFile_CPlus();
	end = GetTickCount();
	printf("C++读二进制文件用时:%dms\n", end - start);*/
	int totalcount = 0;
	//读取文件写入文件
	DWORD t1 = GetTickCount();
	FILE* fp = fopen("D:\\software\\qt-opensource-windows-x86-mingw530-5.7.1.exe", "rb");
	if (!fp)
	{
		return -1;
	}
	char buf[BUFFERSIZE];
	int len = 0;
	while(!feof(fp))
	{
		len = fread(buf, 1, BUFFERSIZE, fp);
	}
	fclose(fp);
	DWORD t2 = GetTickCount();
	cout << "c语言读二进制读文件耗时" << t2-t1<<"ms"<<endl;
	t1 = GetTickCount();
	fstream file;
	file.open("D:\\software\\qt-opensource-windows-x86-mingw530-5.7.1.exe", ios::in | ios::out | ios::binary);
	//char str[BUFFERSIZE];
	while (!file.eof())
	{
		file.read(buf, BUFFERSIZE);
	}
	file.close();
	t2 = GetTickCount();
	cout << "c++读二进制文件耗时" << t2 - t1 << "ms" << endl;
	
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值