Chinaunix首页 | 论坛 | 博客
  • 博客访问: 171470
  • 博文数量: 52
  • 博客积分: 770
  • 博客等级: 军士长
  • 技术积分: 439
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-10 11:34
文章分类

全部博文(52)

文章存档

2014年(11)

2013年(21)

2012年(4)

2011年(8)

2010年(8)

我的朋友

分类:

2010-12-29 11:59:14

, unix2dos 用在 DOS <=> UNIX text file 转换
DOS 格式 0d 0a
UNIX 格式 0a
有时手边无此工具, 可用功能相同的指令组合
dos2unix:
sed -i” "s/\r//g" file
or
cat file | col -b > newfile
or
cat file | tr -d "\r" > newfile
cat file | tr -d "\015" > newfile

unix2dos:
sed -i” "s/$/\r/" file
sed -i” "s/$/\x0d/" file

-i后面的是单引号组成

以上适用 GNU sed, FreeBSD 下的 sed 不适用

查找至::http://pank.org/blog/archives/000209.html

4.1 unix2dos.c程序模块

unix2dos.c程序模块:用来把UNIX格式的文件转换为DOS/WINDOWS格式的文件。
unix2dos.c程序模块源代码如下所示:
/* $Date: 2001/03/28 12:19:21 $
*
* UNIX to DOS text format conversion.
*
* $Log: unix2dos.c,v $
* Revision 1.1 2001/03/28 12:19:21 vong
* Initial revision
* Written by Haowen Huang
*/
static const char rcsid[] =
"$Id: unix2dos.c,v 1.1 2001/03/28 12:19:21 vong Exp $";

#include
#include
#include
#include "file_transfer.h"

/*
* Convert file named by pathname to DOS
* text format, on standard output:
*/
int
unix2dos(const char *pathname) {
int ch;           /* Current input character */
FILE *in = 0;        /* Input file */

if ( !(in = fopen(pathname,"r")) ) {
fprintf(stderr,"Cannot open input file.\n");
return 2;
}
while ( (ch = fgetc(in)) != EOF ) {
if ( ch == ‘\n’ )
putchar(‘\r’);
putchar(ch);
}
fclose(in);
return 0;
}

4.2 dos2unix.c程序模块

dos2unix.c程序模块:用来把DOS/WINDOWS格式的文件转换为UNIX格式的文件。
dos2unix.c程序模块源代码如下所示:

/* $Date: 2001/03/28 12:29:37 $
*
* The DOS to UNIX text conversion:
*
* $Log: dos2unix.c,v $
* Revision 1.1 2001/03/28 12:29:37 vong
* Initial revision
* Written by Haowen Huang
*/
static const char rcsid[] =
"$Id: dos2unix.c,v 1.1 2001/03/28 12:29:37 vong Exp $";

#include
#include
#include
#include "file_transfer.h"

/*
* Convert file named by pathname, to
* UNIX text file format on standard output:
*/
int
dos2unix(const char *pathname) {
int ch;           /* Current input character */
int cr_flag;         /* True when CR prev. encountered */
FILE *in = 0;        /* Input file */

if ( !(in = fopen(pathname,"r")) ) {
fprintf(stderr,"Cannot open input file.\n");
return 2;
}
cr_flag = 0; /* No CR encountered yet */
while ( (ch = fgetc(in)) != EOF ) {
if ( cr_flag && ch != ‘\n’ ) {
/* This CR did not preceed LF */
putchar(‘\r’);
}
if ( !(cr_flag = ch == ‘\r’) )
putchar(ch);
}
fclose(in);
return 0;


阅读(1110) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-01-02 12:11:57

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com