• 大小: 5KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C/C++
  • 标签: 文件读写  

资源简介

操作系统试验之第四文件读写的三种方式比较

资源截图

代码片段和文件信息

#include
#include “stdafx.h“

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DWORD BufferSize=1024;
char buf[1024];

CWinApp theApp;
using namespace std;

//函数声明
void FileReadWrite_NoBuffer(char* sourcechar* destination);//无缓冲方式实现文件读写
void FileReadWrite_FastBuffer(char* sourcechar* destination);//高速缓存方式实现文件读写
void FileReadWrite_Asyn(char* sourcechar* destination);//异步方式实现文件读写

int _tmain(int argc TCHAR* argv[] TCHAR* envp[])
{
int nRetCode = 0;
    DWORD ticktick_s;
DWORD nobuffer_start_timesequential_start_timeoverlapped_start_time;
DWORD nobuffer_end_timesequential_end_timeoverlapped_end_time;
DWORD nobuffer_average_time=0;
DWORD sequential_average_time=0;
DWORD overlapped_average_time=0;

printf(“无缓冲方式运行中...\n“);
nobuffer_start_time=GetTickCount();
for (int i=0;i<10;i++)
{
tick=GetTickCount();
FileReadWrite_NoBuffer(“source.txt““1_nobuffer.txt“);
printf(“无缓冲方式: %d %d ms\n“iGetTickCount()-tick);
}
nobuffer_end_time=GetTickCount();

printf(“\n高速缓存方式运行中...\n“);
    sequential_start_time=GetTickCount();
for (i=0;i<10;i++)
{
tick_s=GetTickCount();
FileReadWrite_FastBuffer(“source.txt““2_FastBuffer.txt“);
printf(“高速缓存方式: %d %d ms\n“iGetTickCount()-tick_s);
}
sequential_end_time=GetTickCount();

printf(“\n异步方式运行中...\n“);
overlapped_start_time=GetTickCount();
for (i=0;i<10;i++)
{
tick=GetTickCount();
FileReadWrite_Asyn(“source.txt““3_Asyn.txt“);
printf(“异步方式: %d %d ms\n“iGetTickCount()-tick);
}
overlapped_end_time=GetTickCount();
printf(“\n“);
printf(“\n最终结果:\n“);
printf(“1.无缓冲方式实现文件读写: %d ms\n“(nobuffer_end_time-nobuffer_start_time)/10);
printf(“2.高速缓存方式实现文件读写: %d ms\n“(sequential_end_time-sequential_start_time)/10);
printf(“3.异步方式实现文件读写: %d ms\n“(overlapped_end_time-overlapped_start_time)/10);

return nRetCode;
}

//无缓冲方式实现文件读写
void FileReadWrite_NoBuffer(char* sourcechar* destination){
HANDLE handle_srchandle_dst;
DWORD NumberOfByteReadNumberOfByteWrite;
BOOL cycle;
char *buffer;
buffer=buf;
handle_src=CreateFile(sourceGENERIC_READ0NULLOPEN_ALWAYSFILE_FLAG_NO_BUFFERINGNULL);
handle_dst=CreateFile(destinationGENERIC_WRITENULLNULLCREATE_ALWAYSNULLNULL);
if(handle_src==INVALID_HANDLE_VALUE||handle_dst==INVALID_HANDLE_VALUE){
printf(“File Create Fail!\n“);
exit(1);
}
cycle=TRUE;
while(cycle){
NumberOfByteRead=BufferSize;
if(!ReadFile(handle_srcbufferNumberOfByteRead&NumberOfByteReadNULL)){
printf(“Read File Error!%d\n“GetLastError());
exit(1);
}
if(Numb

评论

共有 条评论