在 C++ 中打开文件的不同方式在 Windows 上?

发布于 2024-12-04 12:38:31 字数 292 浏览 0 评论 0 原文

当使用fopenfstream时,有必要指定二进制模式以防止字符转换。在类似 UNIX 的系统上,这通常不是必需的,但在 Windows 上却是必需的。

这让我想知道这是否仍然是访问该操作系统上的文件系统数据的“自然”方式。 UNIX 具有“一切都是文件”的体系结构,那么是否有一些 Windows 相当于较低级别的设备来访问数据?毕竟,如果正在进行字符转换,则会产生检查每个字符以确定是否需要转换的开销。希望以二进制模式打开文件只会禁用任何此类处理,但我有疑问。

有人能解释一下吗?

When using fopen or fstream, it's necessary to specify binary mode in order to prevent character conversions. On UNIX like systems this isn't usually necessary but on Windows it is.

This leads me to wonder if this is still the "natural" way to access filesystem data on this operating system. UNIX has the "everything is a file" architecture going for it, so is there some windows equivalent of a lower level device to access the data? After all, if there are character conversions going on there's the overhead involved in checking each character to see if it needs to be converted. Hopefully opening a file in binary mode would simply disable any such processing but I have my doubts.

Could anybody shed some light on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋千易 2024-12-11 12:38:31

您可以只使用 Windows 文件句柄。 创建文件ReadFile写入文件

这与 Unix 的做事方式非常相似。使用这些函数可以完成很多事情。不介意插座,但它是您能得到的最接近的!

You could just use windows file handles. CreateFile, ReadFile and WriteFile.

This is pretty similar to the Unix way of doing things. Lots of things can be done using these functions. Not sockets mind but its about as close as you'll get!

从此见与不见 2024-12-11 12:38:31

C 运行时库的功能与 C 运行时库的功能相同。

Windows 实际上将大多数 的东西作为文件(或者我应该说对象) Windows 中的文件名实际上是全局命名空间 \??\ 中的一个对象。如果您在 CreateFile 的文档="nofollow">MSDN 还有更多内容,特别是如果您深入研究Windows 内核文档

如果您对 Windows 上的对象感到好奇,还可以从 WinObj “nofollow”>技术网

根据您所从事的项目类型,我会在 IO 周围放置一个抽象层,以便能够充分利用 Windows 中的内容,但不能充分利用 C 运行时库中的内容。我很少为不同平台编写代码而不通过每个平台上的本机 API(或通过一些中间抽象)。

the C run-time libraries does what the C run-time libraries does.

Windows actually has most things as a file (or should I say objects) a file name in Windows is actually an object in the global namespace \??\. If you look up the documentation for CreateFile on MSDN there's a lot more to it, especially if you go digging through the Windows kernel documentation.

If you're curious about objects on Windows, you can also download and run WinObj from technet.

Depending on what type of project your working on, I'd put an abstraction layer around IO just to be able to fully utilize what's in Windows but not in the C run-time libraries. I rarely write code for different platforms not going through the native API on each platform (or through some intermediary abstraction).

闻呓 2024-12-11 12:38:31

是的,有 Win32 特定的函数。纯粹主义者会说这会使你的代码不可移植。确实如此!但谁在乎呢,你正在编写一个 Windows 程序。

打开文件CreateFile等等

Yeah, there are Win32-specific functions. Purists will say it makes your code non-portable. And it does! But who cares, you're writing a Windows program.

OpenFile, CreateFile, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文