如何在写入文件时放置换行符 [英] How Can I place a Line Break while writing to a file

查看:677
本文介绍了如何在写入文件时放置换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个配置文件,点击下面的代码按钮。
$ b

  fputs(AutoStart =+ strAutoWinStart + 
AutLogHistory =+ strAutoLogSvrHistory +
AutoScan =+ strAutoScanDetect +
AutoMount =+ strAutoMount +
AutoOpen =+ strAutoOpenWin +
LastConnectedSvr =+ strAutoDetLastConSvr,pFile);

和输出文件如下所示:

< blockquote>

AutoStart = 1AutLogHistory = 0AutoScan = 1AutoMount = 0AutoOpen = 1LastConnectedSvr = 0

相反,我需要输出在每个参数中应该有一个换行符,如下所示:


AutoStart = 1

AutLogHistory = 0 >
AutoScan = 1

AutoMount = 0

AutoOpen = 1

LastConnectedSvr = 0



解决方案

在UNIX样式的换行符中添加\r\\\
(或\\\
)以在文件。



Windows上的某些编辑器(如记事本)不能很好地处理UNIX样式的换行符,只显示一行内容。为了实现跨操作系统的最大可移植性,请使用\ n。为了在Windows中的最大可用性,使用\r\\\


更好:使用std :: ofstream和std :: endl - 你的问题被标记C ++毕竟。


I am creating a configuration file on click of a button by the following code.

fputs(  "AutoStart = " +strAutoWinStart + 
        "AutLogHistory = " +strAutoLogSvrHistory + 
        "AutoScan= " +strAutoScanDetect + 
        "AutoMount = "+ strAutoMount + 
        "AutoOpen = "+ strAutoOpenWin + 
        "LastConnectedSvr = "+ strAutoDetLastConSvr,pFile);

and the Output file looks as below:

AutoStart = 1AutLogHistory = 0AutoScan= 1AutoMount = 0AutoOpen = 1LastConnectedSvr = 0

Instead I need my output should have a line break in each parameter and look as below:

AutoStart = 1
AutLogHistory = 0
AutoScan= 1
AutoMount = 0
AutoOpen = 1
LastConnectedSvr = 0

解决方案

Add "\r\n" (or "\n" for UNIX-style linefeeds) to create a new line in the file.

Some editors on Windows such as Notepad don't handle UNIX-style linefeeds well, showing everything on one line. For the most portability across operating systems, use "\n". For the most usability in Windows, use "\r\n".

Even better: use std::ofstream with std::endl - your question is tagged C++ after all.

这篇关于如何在写入文件时放置换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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