今天寫了一個UNIX2DOS工具,用于把UNIX系統的文本轉換成DOS(Windows)下支持的文本。
兩大操作系統文本主要的不同在于換行時UNIX里只有 '\n'字符,而在DOS(Windows)下變成了'\r\n'。
廢話不多說,此工具用C語言寫成,源代碼如下:
?
//
#include? < stdio.h >
#include? < stdlib.h >
int ?main( int ?argc, char ? * argv[])
{
???? int ?ch;
????FILE? * fpinPtr, * fpoutPtr;
???? if ?(argc != 3 )
????{
????????printf( " UNIX2DOS?program.\n\n " );
????????printf( " Usage:?command?source_file?target_file\n " );
????????printf( " Usage?example:?\ " unix2dos?src.txt?obj.txt\ " \n " );
????????exit(EXIT_FAILURE);
????}
???? if ?((fpinPtr = fopen(argv[ 1 ], " rb " )) == NULL)
????{
????????printf( " Input?file?\ " % s\ " ?could?not?be?opened\n " ,argv[ 1 ]);
????????exit(EXIT_FAILURE);
????}
???? if ?((fpoutPtr = fopen(argv[ 2 ], " wb " )) == NULL)
????{
????????printf( " Outout?file?\ " % s\ " ?could?not?be?opened\n " ,argv[ 2 ]);
????????exit(EXIT_FAILURE);
????}
???? while ( ! feof(fpinPtr))
????{
????????ch = fgetc(fpinPtr);
???????? if (ch >- 1 ? && ?ch? != ? ' \n ' )
????????{
????????????fputc(ch,fpoutPtr);
????????}
???????? else ? if (ch >- 1 )
????????{
????????????fputc( ' \r ' ,fpoutPtr);
????????????fputc(ch,fpoutPtr);
????????}
????}
????fclose(fpinPtr);
????fclose(fpoutPtr);
???? return ? 0 ;
}
?
當然,還有一個附加產品:DOS2UNIX,源代碼如下:
//
#include? < stdio.h >
#include? < stdlib.h >
int ?main( int ?argc, char ? * argv[])
{
???? int ?ch;
????FILE? * fpinPtr, * fpoutPtr;
???? if ?(argc != 3 )
????{
????????printf( " DOS2UNIX?program.\n\n " );
????????printf( " Usage:?command?source_file?target_file\n " );
????????printf( " Usage?example:?\ " DOS2UNIX?src.txt?obj.txt\ " \n " );
????????exit(EXIT_FAILURE);
????}
???? if ?((fpinPtr = fopen(argv[ 1 ], " rb " )) == NULL)
????{
????????printf( " Input?file?\ " % s\ " ?could?not?be?opened\n " ,argv[ 1 ]);
????????exit(EXIT_FAILURE);
????}
???? if ?((fpoutPtr = fopen(argv[ 2 ], " wb " )) == NULL)
????{
????????printf( " Outout?file?\ " % s\ " ?could?not?be?opened\n " ,argv[ 2 ]);
????????exit(EXIT_FAILURE);
????}
???? while ( ! feof(fpinPtr))
????{
????????ch = fgetc(fpinPtr);
???????? if (ch >- 1 ? && ?ch? != ? ' \r ' )
????????{
????????????fputc(ch,fpoutPtr);
????????}
????}
????fclose(fpinPtr);
????fclose(fpoutPtr);
???? return ? 0 ;
}
?
參考代碼源:HP UNIX CP命令?,歡迎各位高手批評指正。?
Wednesday, May 06, 2009
?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
