首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
Google
首页 入门 Linux编程 系统管理 网络管理 Linux认证 Unix/BSD Linux数据库 Linux集群 Linux手册 Linux下载 Ubuntu 论坛 专题 RSS
您现在的位置: 中国IT实验室 >> Linux >> 系统管理 >> 系统管理 >> 正文

linux与windows回车换行问题

  所谓回车、换行这些控制符,都是从以前的电传打字机的控制命令继承下来的。回车就是打印头复位,换行就是走纸。Dos/Windows和Unix/Linux对回车、换行的理解差别就在于Dos/Windows认为0d=0d0a=0a,而Unix/Linux坚持沿用电传打字机的工作方式(这个其实是比较正确的)。
 
  所以在回车换行在Linux中是"0d",在Windows中是"0d0a".我们可以通过下面的程序测试一下:

    CODE:
 
  

#define MAX_LENGTH 15536
#include 
#include 
using namespace std;
string delEnter(const string src) // 过滤掉串中的回车换行符
{
string des;
for(int i = 0; i < src.length(); i++)
{
char tempChar = src[i];
if( tempChar!=10 && tempChar!=13)
des.append(1,tempChar);
} 
return des;
} 
int main()
{
char html[MAX_LENGTH] = "";
FILE *fp = fopen("Linux.txt", "rb"); //FILE *fp = fopen("Windows.txt", "rb");
char buf[16384];
while (fgets(buf, 16384, fp))
strcat(html, buf);
strcat(html,"\0");
string s(html);
cout << "string is: " << s << endl;;
cout << "The size of string is: " << s.length() << endl;
cout << "after del string is: " << delEnter(s) << endl;
cout << "The size of string is: " << delEnter(s).length() << endl;
fclose(fp);
return 0;
}


 
  程序中文件Linux.txt是从Linux系统中copy过来的。我们可以通过这个程序观察到,通过过滤掉回车换行符,Linux文件中的字符数的减少等于其行数,而
 
  Windows中等于其行数的两倍。但有一个问题要注意,程序中行:
 
  CODE:
 
  

FILE *fp = fopen("Linux.txt", "rb");
不能写成:
FILE *fp = fopen("Linux.txt", "r");


 
  后者默认的文件打开方式是文本方式,这时系统自动对文本进行了转换,就不能得到上述的结论。
 

【责编:Zenghui】
中国IT教育
相关产品和培训
文章评论
 友情推荐链接
 专题推荐

 ·防范Linux病毒 打造没有病毒的乐土…
 ·Linux Shell编程实用指南…
 ·Linux日志分析与管理
 ·揭密Linux内存管理
 ·邮件服务Sendmail应用配置
 ·Linux 安全管理…
 ·Linux 下DNS服务器架设攻略…
 ·Linux 下的路由的配置与应用…
 ·专题:Apache实用手册
 ·全面剖析Linux文件系统
 今日更新
 认证培训
 频道精选
 Windows频道导航