LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2954|回复: 9

linux和dos(windows)的回车符号区别是什么?

[复制链接]
发表于 2002-10-15 23:57:05 | 显示全部楼层 |阅读模式
linux和dos的下回车符(或者叫换行)到底有什么区别?
发表于 2002-10-16 00:01:04 | 显示全部楼层
dos/win下是0x0d 0x0a
unix下是0x0a
所以如果用Windows的记事本查看Linux下写的文本会很乱。
 楼主| 发表于 2002-10-16 12:59:47 | 显示全部楼层

还是有问题

可是我试过在dos下用type命令显示linux的kedit编写的文本,和linux下的没有什么两样啊?
    我查过ASCII代码表,0x0d代表“carriage return”,这个carriage return是什么意思?
    多谢!
 楼主| 发表于 2002-10-31 10:07:44 | 显示全部楼层

问题已经解决。

我编写了一个解决此问题的程序,可以转换两种格式的文件。由于使用的是标准的c函数,所以它是广泛适用的。有问题告诉我。

#include"stdio.h"
#include"string.h"

void usage()            /* show the usage of the program */
{
printf("\n  Usage\t\t ctxfm -u|-U|-d|-D infile outfile\n\n");
printf("  -u -U\tturn a dos text into unix form.\n");
printf("  -d -D\tturn a dos text into unix form.\n\n");
exit(0);
}

/* turn a dos text into unix form */
void turnunix(char *infile,char *outfile)
{FILE *in,*out;
char ch,flag=0;
if((in=fopen(infile,"rb"))==NULL)             /* open infile */
        {printf("Cannot open file %s\n",infile);
        exit(0);
        }
if((out=fopen(outfile,"rb"))!=NULL)            /* open outfile */
        {fclose(out);
        printf("ERROR! Outfile %s has been existened!",outfile);
        exit(0);}
if((out=fopen(outfile,"wb"))==NULL)
        {printf("Cannot open outfile %s\n",outfile);
        exit(0);
        }
while(1)                                       /* write the outfile */
        {ch=fgetc(in);
        if(feof(in))break;
        if(ch=='\r')flag=1;              /*in dos is \r\n*/
        else if((ch=='\n')&&flag)fseek(out,-1L,1);
                         else flag=0;
        fputc(ch,out);
        }
if(fclose(in)==EOF||fclose(out)==EOF)          /* close the file */
        {printf("File close error!\n");
        exit(0);
        }
}

/* turn a unix text form into dos form */
void turndos(char *infile,char *outfile)
{FILE *in,*out;
char ch;
if((in=fopen(infile,"rb"))==NULL)             /* open infile */
        {printf("Cannot open file %s\n",infile);
        exit(0);
        }
if((out=fopen(outfile,"rb"))!=NULL)            /* open outfile */
        {fclose(out);
        printf("ERROR! Outfile %s has been existened!",outfile);
        exit(0);}
if((out=fopen(outfile,"wb"))==NULL)
        {printf("Cannot open outfile %s\n",outfile);
        exit(0);
        }
while(1)                                       /* write the outfile */
        {ch=fgetc(in);
        if(feof(in))break;
        if(ch=='\n')                                                                   /* in unix is \n */
                fputs("\r\n",out);
        else fputc(ch,out);
        }
if(fclose(in)==EOF||fclose(out)==EOF)          /* close the file */
        {printf("File close error!\n");
        exit(0);
        }
}

void main(int argc,char *argv[])
{
/* check parameters */
if(argc!=4)usage();

/*deal with different cases*/
if(!strcmp(argv[1],"-u")||!strcmp(argv[1],"-U"))
        turnunix(argv[2],argv[3]);
else if(!strcmp(argv[1],"-d")||!strcmp(argv[1],"-D"))
                 turndos(argv[2],argv[3]);
else usage();
}
发表于 2002-10-31 18:43:47 | 显示全部楼层
真的很不错,我在Linux下也测试成功,只要把void main中的void去掉。 tyl1983兄弟的这种学习精神很值得学习。
发表于 2002-11-1 09:51:13 | 显示全部楼层

其实有个现成命令!

mcopy

从DOS文件系统拷贝文件或把文件拷贝到DOS文件系统。

语法:

mcopy[参数]源文件目标文件

-t文本文件传输。Mcopy将回车/换行翻译成换行。

-n当覆盖一个已存在的文件时不警告用户。

-m保存文件修改时间。
发表于 2002-11-1 13:34:37 | 显示全部楼层
dos2unix
unix2dos
 楼主| 发表于 2002-11-1 21:57:36 | 显示全部楼层

原来有现成的命令

看来我还要好好学习一下在linux的命令行界面下工作。
发表于 2002-11-4 10:34:43 | 显示全部楼层

还有别的办法,也可解决这个问题。

讨厌的^M 字符,有两个简单的方法可以取消它。

1 用"vi"打开此文件,在Command mode下敲入:

:%s/^V^M//g

2 在SHELL下敲入:

sed 's/^V^M//g' foo > foo.new。
发表于 2002-11-8 16:52:32 | 显示全部楼层
其实用perl也很容易就能搞定的,用chop把最后一个字符砍掉就是了,呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表