为什么cout<<"/n";就换行了?

femalelover 2006-01-11 01:41:15
为什么控制台下cout<<"/n";就换行了,
而在MFC中如果要在编辑框里换行得/r/n呢,不都是Windows吗?
...全文
861 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
MountLion 2006-01-12
  • 打赏
  • 举报
回复
抄英文做什么,楼主若愿意看英文,也不会来问了。

问题的原因是:
在Dos下,I/O函数处理文本文件时会自动转换\n和\r\n,在读取时,把\r\n转换成\n,写入时,把\n转换为\r\n。
printf使用的是标准输出stdout,stdout是以文本方式打开的文件(设备),因此会涉及自动转换;windows属性框与标准输入输出无关,因此不能自动转换。
积木 2006-01-12
  • 打赏
  • 举报
回复
windows那个破玩意,不符合标准的地方多了,不过用的人多,人家就是事实标准。你也不能说什么。
  • 打赏
  • 举报
回复
原因是: If you open a file in translated mode, the runtime library will convert a '\n' to the appropriate newline character(s).

------------------------------------------

There are a few characters which can indicate a new line. The usual ones are these two:

'\n' or '0x0A' (10 in decimal) -> This character is called "Line Feed" (LF).
'\r' or '0x0D' (13 in decimal) -> This one is called "Carriage return" (CR).

Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones:
DOS and Windows

They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).


Unix (and hence Linux as well)

Unix uses a single '\n' to indicate a new line.


Mac

Macs use a single '\r'.

This difference gives rise to a number of problems. For example, a file created under Unix (so with newlines as a single LF) will not open correctly under Window's Notepad. Any Windows program that expects newlines to be CRLF will not work correctly with these files.

To unify things a bit, so that writing portable C/C++ programs is possible, file streams have both a "translated" and an "untranslated" mode. If you open a file in translated mode, the runtime library will convert a '\n' to the appropriate newline character(s). If the following program is compiled under Unix, the file will contain a single LF to indicate the newline. If it's compiled under windows, it will contain a CRLF.


Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp = fopen("testfile.txt", "w");
fprintf(fp, "Hello World\n");
fclose(fp);
return 0;
}
---------------------------------------
http://www.codeguru.com/forum/showthread.php?s=&threadid=253826
cky41 2006-01-11
  • 打赏
  • 举报
回复
虽然都是windows但事识别的换行符不一样,一般的控制台都是\n就换行,但是在windows的图形界面中一般都是\r\n才换行(也就是平常所说的回车换行)

如果是linux,那么只要\n就换行
caijize 2006-01-11
  • 打赏
  • 举报
回复
你都会说那是近控制台啦~~
跟编辑框怎么会一样呢??
femalelover 2006-01-11
  • 打赏
  • 举报
回复
明明要求\r\n才另起一行的,为什么控制台只\n一下就行了,
去查cout,也没看见有默认\r的意义.
femalelover 2006-01-11
  • 打赏
  • 举报
回复
晕,不要老是纠缠着我的那个反斜杠不放呀.
我知道也可以在输出时用cout<<"\r\n",
可是,为什么在MFC的编辑框中只用\n就不行呢.
PMsg 2006-01-11
  • 打赏
  • 举报
回复
既然用流就用endl吗~
fiftymetre 2006-01-11
  • 打赏
  • 举报
回复
\n
cdo 2006-01-11
  • 打赏
  • 举报
回复
你也可以统一都使用“\r\n”呀。
iamcaicainiao 2006-01-11
  • 打赏
  • 举报
回复
so it is
yxg80 2006-01-11
  • 打赏
  • 举报
回复
在“/n”输出的是:/n,cout<<"\n"就是换行了,楼上说的对!
femalelover 2006-01-11
  • 打赏
  • 举报
回复
编辑框的话你要在properties里面的styles中 multiline打上勾,表示支持多行
lz给分
=============
这个根本就不符问题,选中多行属性编辑框可接受多行谁不知道.

理论上,Windows下都得要\r\n回车加换行符结合使用才能另起一行,Unix/linux下只用\n就可以了,现在问题是Windows下cout时,一个\n就行了,与Unix一样了.Why?
我只想知道为什么会这样,楼上所有的答案好像都没说到点上.
haizi0612 2006-01-11
  • 打赏
  • 举报
回复
楼上说得对啊
robertcarlos 2006-01-11
  • 打赏
  • 举报
回复
编辑框的话你要在properties里面的styles中 multiline打上勾,表示支持多行
lz给分
pbdwadr 2006-01-11
  • 打赏
  • 举报
回复
chengzanmiao(高薪為共產當多納稅)

\r 回车,光标定位到当前行(不是下一行)的开始位置。
\n 新行,光标定位到下一行的开始位置。
printf,cout等一般的控制台都是支持(\r\n)直接为\n,而编辑框不支持
----------------------------------------------------------------

正是!!
cunsh 2006-01-11
  • 打赏
  • 举报
回复
不是 /
是 \
ayamaoaya 2006-01-11
  • 打赏
  • 举报
回复
\n代表转义字符吧?意思就是换行?
chengzanmiao 2006-01-11
  • 打赏
  • 举报
回复
\r 回车,光标定位到当前行(不是下一行)的开始位置。
\n 新行,光标定位到下一行的开始位置。
printf,cout等一般的控制台都是支持(\r\n)直接为\n,而编辑框不支持
femalelover 2006-01-11
  • 打赏
  • 举报
回复
其实我想要的回答的为什么printf,cout等控制台下不用回车符而直接用换行符就能另起一行了.

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧