C ++将换行符从CR + LF更改为LF [英] C++ change newline from CR+LF to LF

查看:409
本文介绍了C ++将换行符从CR + LF更改为LF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写在Windows中运行的代码,并输出一个文本文件,该文本文件后来成为Linux中程序的输入.当给定文件的换行符是CR + LF而不是LF时,此程序的行为将不正确.

I am writing code that runs in Windows and outputs a text file that later becomes the input to a program in Linux. This program behaves incorrectly when given files that have newlines that are CR+LF rather than just LF.

我知道我可以使用诸如dos2unix之类的工具,但是我想跳过多余的步骤.是否可以在Windows中获得C ++程序以使用Linux换行符而不是Windows换行符?

I know that I can use tools like dos2unix, but I'd like to skip the extra step. Is it possible to get a C++ program in Windows to use the Linux newline instead of the Windows one?

推荐答案

是的,您必须以二进制"模式打开文件才能停止换行.

Yes, you have to open the file in "binary" mode to stop the newline translation.

操作方式取决于打开文件的方式.

How you do it depends on how you are opening the file.

使用fopen:

FILE* outfile = fopen( "filename", "wb" );

使用ofstream:

std::ofstream outfile( "filename", std::ios_base::binary | std::ios_base::out );

这篇关于C ++将换行符从CR + LF更改为LF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆