如何阻止 Windows C 库将“\r\n”转换为“\r\n”到“\n”当用C读取文本文件时?

发布于 2024-12-13 14:44:06 字数 313 浏览 3 评论 0原文

我有一个普通的文本文件,其行以普通 \r\n 结尾。然而,当使用'open'和'read'时,Windows将所有\r\n转换为\n。我知道这意味着我必须以二进制模式打开文件,但函数“open”没有提供此选项,它只有只读、只写或读写。

这是代码:

int File_Size = ...; 
char* Buffer = (char*)malloc(File_Size);

int Handle = open(File_Path,O_RDONLY);
read(Handle,Buffer,File_Size);
close(Handle);

I have a normal text file that lines end with normal \r\n. However, when using 'open' and 'read', Windows convert all the \r\n to \n. I know this means I have to open the file in binary mode but the function 'open' doesn't give this option, it has only read-only, write-only, or read-write.

This is the code:

int File_Size = ...; 
char* Buffer = (char*)malloc(File_Size);

int Handle = open(File_Path,O_RDONLY);
read(Handle,Buffer,File_Size);
close(Handle);

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

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

发布评论

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

评论(1

后eg是否自 2024-12-20 14:44:06

尝试使用O_RDONLY|O_BINARY

Try using O_RDONLY|O_BINARY.

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