简体   繁体   English

C 样式,C++ 流或 Win32 API 文件 I/O?

[英]C style, C++ streams or Win32 API File I/O?

I read C++ Streams vs. C-style IO?我读过C++ Streams 与 C 风格 IO? (amongst other pages) to try to help me decide which way to implement some file IO in a project I'm working on. (在其他页面中)尝试帮助我决定在我正在处理的项目中实现某些文件 IO 的方式。

Background I'm fairly new to C++ and Windows programming, I've traditionally worked in C and command line applications.背景我对 C++ 和 Windows 编程相当陌生,我传统上曾在 C 和命令行应用程序中工作。 Apologies ahead of time for the n00b-ness of this question.提前为这个问题的n00b-ness道歉。

The problem I want to read one text file, process the contents and output to another (new) text file.我想读取一个文本文件,将内容和 output 处理到另一个(新)文本文件的问题 I am working in a Win32 environment (and this won't change for the forseeable future) and am writing the application to be Unicode aware, through _T style macros.我在 Win32 环境中工作(这在可预见的未来不会改变)并且正在编写应用程序以通过 _T 样式宏了解 Unicode。 The "processing" could include inserting/appending/deleting the lines of text, which will be at most 128 characters. “处理”可以包括插入/附加/删除文本行,最多为 128 个字符。

The question I would prefer to write something that is going to be robust, so I/O error handling is a consideration.这个问题我更愿意写一些健壮的东西,所以 I/O 错误处理是一个考虑因素。 I think that I need to stay away from C style file I/O if for no other reason than to simplify the code and type checking -- ie approach this in a more OO POV.我认为我需要远离 C 样式文件 I/O,如果没有其他原因,只是为了简化代码和类型检查——即在更 OO POV 中处理这个问题。 What are the advantages of using Win32 API functions over the C++ stream functions (if any)?与 C++ stream 函数(如果有)相比,使用 Win32 API 函数有哪些优势? Can you recommend a good primer for either approach?你能为这两种方法推荐一个好的入门书吗? (My googling has left me with a little information overload) (我的谷歌搜索给我留下了一点信息过载)

Thanks muchly非常感谢

What are the advantages of using Win32 API functions over the C++ stream functions (if any)?与 C++ stream 函数(如果有)相比,使用 Win32 API 函数有哪些优势?

  1. Speed速度
  2. Ability to use overlapped I/O to handle multiple operations at once without threads (and the complexity of synchronization)能够使用重叠 I/O 在没有线程的情况下一次处理多个操作(以及同步的复杂性)
  3. Speed速度
  4. More specific error codes更具体的错误代码
  5. Speed速度
  6. Speed速度
  7. Low dependency footprint (compared to MSVC++ 7.x, 8.0, 9.0, 10.0 and probably most other vendors)低依赖足迹(与 MSVC++ 7.x、8.0、9.0、10.0 以及可能的大多数其他供应商相比)
  8. Speed速度

Use C++ stream I/O.使用 C++ stream I/O。 Writing to text files is hardly going to stress the I/O library, and you gain enormous benefits in clarity of code, type safety, and the fact that you hardly have to write anything to get the job done.写入文本文件几乎不会对 I/O 库造成压力,而且您在代码清晰、类型安全以及几乎不需要编写任何内容即可完成工作这一事实方面获得了巨大的好处。 As a side effect, your code will probably be more portable and more understandable, so if you have to ask about it here, you will get more good answers.作为一个副作用,你的代码可能会更便携和更容易理解,所以如果你必须在这里询问它,你会得到更多好的答案。

To take a broader look, direct use of Win32 is good if you need a tiny application with no additional dependencies.从更广泛的角度来看,如果您需要一个没有额外依赖项的小型应用程序,直接使用 Win32 会很好。

For anything that C++ iostreams does better, you probably want to look at Boost::Spirit.对于 C++ iostreams 做得更好的任何事情,您可能想看看 Boost::Spirit。 Seems like it has all the type-safety of iostreams, with much better performance.似乎它具有 iostream 的所有类型安全性,并且性能要好得多。

You really have two problems here: File I/O, and Text Processing.您在这里确实有两个问题:文件 I/O 和文本处理。 Win32 does the first exceptionally well, and provides no help with the second. Win32 在第一个方面做得非常好,对第二个没有帮助。 Boost::Spirit does the second very well. Boost::Spirit 在第二个方面做得很好。 C++ iostreams are marginal at both tasks, avoid them unless portability is the most important feature. C++ iostreams 在这两个任务中都是边缘的,除非可移植性是最重要的特性,否则请避免使用它们。

Just to provide a rough benchmark - this code, which must be about the most inefficient possible:只是为了提供一个粗略的基准 - 这段代码必须是效率最低的:

#include <iostream>
using namespace std;

unsigned int MB = 1024 * 1024;
unsigned int GB = MB * 1024;

int main() {
    char c = 'x';
    for ( unsigned int i = 0; i < GB; i++ ) {
        cout << c;
    }
}

Took about 4 minutes to write a gig of data to a text file when invoked as:调用时花了大约 4 分钟将数据写入文本文件:

myprog > file.txt

on my hardly state-of-the-art laptop.在我几乎不是最先进的笔记本电脑上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM