ACE对文件操作方法
来源:互联网 发布:行业经济数据2010 编辑:程序博客网 时间:2024/06/15 17:37
下面是一个使用多种方法进行文件拷贝操作的例子:
// 1、使用操作系统的函数进行逐个字符的拷贝. read/write/lseek
int
Slow_Read_Write_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
ACE_HANDLE ifd = fileno (input_fp);
ACE_HANDLE ofd = fileno (output_fp);
this->tm_.start ();
while (--iterations >= 0)
{
char c;
while (ACE_OS::read (ifd, &c, sizeof c) > 0)
::write (ofd, &c, sizeof c);
ACE_OS::lseek (ifd, 0, SEEK_SET);
ACE_OS::lseek (ofd, 0, SEEK_SET);
}
this->tm_.stop ();
return 0;
}
// 2. 使用标准C函数进行逐字符拷贝. getc/putc/rewind
int
Stdio_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
this->tm_.start ();
while (--iterations >= 0)
{
int c;
while ((c = getc (input_fp)) != EOF)
putc (c, output_fp);
ACE_OS::rewind (input_fp);
ACE_OS::rewind (output_fp);
}
this->tm_.stop ();
return 0;
}
// 3. 块读取. read/write/lseek
int
Block_Read_Write_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
int ifd = fileno (input_fp);
int ofd = fileno (output_fp);
this->tm_.start ();
while (--iterations >= 0)
{
char buf[BUFSIZ];
ssize_t n;
while ((n = ACE_OS::read (ifd,
buf,
sizeof buf)) > 0)
::write (ofd, buf, n);
ACE_OS::lseek (ifd, 0, SEEK_SET);
ACE_OS::lseek (ofd, 0, SEEK_SET);
}
this->tm_.stop ();
return 0;
}
// 4. fread/fwrite/lseek
int
Block_Fread_Fwrite_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
this->tm_.start ();
while (--iterations >= 0)
{
char buf[BUFSIZ];
ssize_t n;
while ((n = ACE_OS::fread (buf,
1,
sizeof buf,
input_fp)) != 0)
::fwrite (buf, n, 1, output_fp);
ACE_OS::rewind (input_fp);
ACE_OS::rewind (output_fp);
}
this->tm_.stop ();
return 0;
}
// 5. 使用内存映射文件打开输入文件,使用write 进行文件写操作
int
Mmap1_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
ACE_Mem_Map map_input (fileno (input_fp));
void *src = map_input.addr ();
if (src == MAP_FAILED)
ACE_ERROR_RETURN ((LM_ERROR,
"%s",
this->name ()),
-1);
else
{
this->tm_.start ();
while (--iterations >= 0)
{
if (ACE_OS::write (fileno (output_fp),
src,
map_input.size ()) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%s",
this->name ()),
-1);
ACE_OS::lseek (fileno (output_fp),
0,
SEEK_SET);
}
this->tm_.stop ();
}
if (map_input.unmap () == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%s",
this->name ()),
-1);
else
return 0;
}
// 6. 使用内存映射文件打开输入、输出文件, 使用memcpy 进行文件内容的拷贝
int
Mmap2_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
ACE_Mem_Map map_input (fileno (input_fp));
int size = map_input.size ();
ACE_Mem_Map map_output (fileno (output_fp),
size,
PROT_WRITE,
MAP_SHARED);
void *src = map_input.addr ();
void *dst = map_output.addr ();
if (src == MAP_FAILED || dst == MAP_FAILED)
return -1;
else
{
this->tm_.start ();
while (--iterations >= 0)
ACE_OS::memcpy (dst, src, size);
this->tm_.stop ();
}
if (map_input.unmap () == -1
|| map_output.unmap () == -1)
return -1;
else
return 0;
}
// 1、使用操作系统的函数进行逐个字符的拷贝. read/write/lseek
int
Slow_Read_Write_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
ACE_HANDLE ifd = fileno (input_fp);
ACE_HANDLE ofd = fileno (output_fp);
this->tm_.start ();
while (--iterations >= 0)
{
char c;
while (ACE_OS::read (ifd, &c, sizeof c) > 0)
::write (ofd, &c, sizeof c);
ACE_OS::lseek (ifd, 0, SEEK_SET);
ACE_OS::lseek (ofd, 0, SEEK_SET);
}
this->tm_.stop ();
return 0;
}
// 2. 使用标准C函数进行逐字符拷贝. getc/putc/rewind
int
Stdio_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
this->tm_.start ();
while (--iterations >= 0)
{
int c;
while ((c = getc (input_fp)) != EOF)
putc (c, output_fp);
ACE_OS::rewind (input_fp);
ACE_OS::rewind (output_fp);
}
this->tm_.stop ();
return 0;
}
// 3. 块读取. read/write/lseek
int
Block_Read_Write_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
int ifd = fileno (input_fp);
int ofd = fileno (output_fp);
this->tm_.start ();
while (--iterations >= 0)
{
char buf[BUFSIZ];
ssize_t n;
while ((n = ACE_OS::read (ifd,
buf,
sizeof buf)) > 0)
::write (ofd, buf, n);
ACE_OS::lseek (ifd, 0, SEEK_SET);
ACE_OS::lseek (ofd, 0, SEEK_SET);
}
this->tm_.stop ();
return 0;
}
// 4. fread/fwrite/lseek
int
Block_Fread_Fwrite_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
this->tm_.start ();
while (--iterations >= 0)
{
char buf[BUFSIZ];
ssize_t n;
while ((n = ACE_OS::fread (buf,
1,
sizeof buf,
input_fp)) != 0)
::fwrite (buf, n, 1, output_fp);
ACE_OS::rewind (input_fp);
ACE_OS::rewind (output_fp);
}
this->tm_.stop ();
return 0;
}
// 5. 使用内存映射文件打开输入文件,使用write 进行文件写操作
int
Mmap1_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
ACE_Mem_Map map_input (fileno (input_fp));
void *src = map_input.addr ();
if (src == MAP_FAILED)
ACE_ERROR_RETURN ((LM_ERROR,
"%s",
this->name ()),
-1);
else
{
this->tm_.start ();
while (--iterations >= 0)
{
if (ACE_OS::write (fileno (output_fp),
src,
map_input.size ()) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%s",
this->name ()),
-1);
ACE_OS::lseek (fileno (output_fp),
0,
SEEK_SET);
}
this->tm_.stop ();
}
if (map_input.unmap () == -1)
ACE_ERROR_RETURN ((LM_ERROR,
"%s",
this->name ()),
-1);
else
return 0;
}
// 6. 使用内存映射文件打开输入、输出文件, 使用memcpy 进行文件内容的拷贝
int
Mmap2_Test::run_test (int iterations,
FILE *input_fp,
FILE *output_fp)
{
ACE_Mem_Map map_input (fileno (input_fp));
int size = map_input.size ();
ACE_Mem_Map map_output (fileno (output_fp),
size,
PROT_WRITE,
MAP_SHARED);
void *src = map_input.addr ();
void *dst = map_output.addr ();
if (src == MAP_FAILED || dst == MAP_FAILED)
return -1;
else
{
this->tm_.start ();
while (--iterations >= 0)
ACE_OS::memcpy (dst, src, size);
this->tm_.stop ();
}
if (map_input.unmap () == -1
|| map_output.unmap () == -1)
return -1;
else
return 0;
}
- ACE对文件操作方法
- PHP对文件的操作方法
- Delphi对INI文件的详细操作方法
- Golang 中对文件 file 操作方法总结
- MFC中对文件的操作方法(转)
- 详细学习PHP中对文件和目录的操作方法
- .net2.0中对config文件的操作方法总结
- .net2.0中对config文件的操作方法总结
- .net2.0中对config文件的操作方法总结
- net2.0中对config文件的操作方法总结
- .net2.0中对config文件的操作方法总结
- .net2.0中对config文件的操作方法总结
- 详细学习PHP中对文件和目录的操作方法
- .net2.0中对config文件的操作方法总结
- 在.net2.0中对config文件的操作方法总结
- .net2.0中对config文件的操作方法总结
- net2.0中对config文件的操作方法总结
- MFC中对文件的操作方法(转)
- android短信和彩信探秘threads
- JRTPLIB 3.5.2手册中文版---part8
- 谈跨平台C++动态连接库的实现
- hust_Sum the Square
- 追求的真的不一样,但能感觉到都很美。
- ACE对文件操作方法
- 详解在QT中写控制台程序 实例
- 如何汉化 Aptana Studio 1.2.7
- hdu 3931 Cross the Fire
- hust_Tri graphs
- weblogic 9.1 热部署
- Linux系统调用
- Windows进程间通信之邮槽
- uva 331 Mapping the Swaps