windows与linux换行符问题 .

来源:互联网 发布:linux软件百度云 编辑:程序博客网 时间:2024/05/16 11:54

在Linux下的换行是/n,而在Windows下的换行是/r/n。不经过处理的话,两者的文件在显示的时候会出现问题,比如一个Linux的文本文件用Windows记事本打开的时候不会换行格式很乱,一个Windows文件在Linux下用VI打开的话会出现^M字符

 

windows文本文件用回车符和换行符的组合/r/n来表示行尾。

linux/unix换行符为/n

Mac文本文件用/r表示行尾

 

C程序使用一个/n来表示行尾,所以,如果C程序以文本视图模式处理一个windows文本文件,在读取文件时它会将/r/n转换成/n,在写入文件的时候会将/n转化成/r/n.

如果使用二进制视图,将不发生任何映射。

下面这个程序用于处理/r/n和/r的转换的问题

[cpp] view plaincopyprint?
  1. /* 
  2.  * Unix/DOS换行符转换程序 
  3.  * auther:cm 
  4.  * date:2011-4-19 
  5.  */  
  6. #include <stdio.h>   
  7. #include <stdlib.h>   
  8. #include <string.h>   
  9. /*函数声明*/  
  10. void usage();   //用法  
  11. void turndos(char *,char *); //将unix格式转换成dos格式  
  12. void turnunix(char *, char *);//将dos格式转换成unix格式  
  13. int main(int argc, char *argv[])  
  14. {  
  15.     if (argc != 4)  
  16.     {  
  17.         //参数错误   
  18.         usage();  
  19.         exit(0);  
  20.     }  
  21.     //printf("%s,%s",argv[2],argv[3]);  
  22.     //参数判断   
  23.     if (!strcmp(argv[1],"-u")||!strcmp(argv[1],"-U"))  
  24.         turnunix(argv[2],argv[3]);  
  25.     else if (!strcmp(argv[1],"-d")||!strcmp(argv[1],"-D"))  
  26.         turndos(argv[2],argv[3]);  
  27.     else  
  28.         usage();  
  29.     return 0;  
  30. }  
  31.   
  32. void usage()  
  33. {  
  34.     printf("/nUsage: ud -u|-U|-d|-D srcfile dstfile/n");  
  35.     printf("/n-u|-U/tturn a dos/windows text to unix/linux form");  
  36.     printf("/n-d|-D/tturn a unix/linux text to dos/windows form");  
  37. }  
  38. void turnunix(char *srcfile,char *dstfile)  
  39. {  
  40.     FILE *in,*out;  
  41.     char buf[1];  
  42.     int read = 0;  
  43.     int flag;  
  44.     //以二进制形式打开文件   
  45.     if ((in=fopen(srcfile,"rb")) == NULL)  
  46.     {  
  47.         printf("/nCan not open file %s",srcfile);  
  48.         exit(-1);  
  49.     }  
  50.     //判断文件是否已存在   
  51.     if ((out=fopen(dstfile,"rb"))!=NULL)  
  52.     {  
  53.         fclose(out);  
  54.         printf("/nERROR! dstfile %s has been existed!",dstfile);  
  55.         exit(-1);  
  56.     }  
  57.     if ((out=fopen(dstfile,"wb"))==NULL)  
  58.     {  
  59.         printf("/nCan not create dstfile %s",dstfile);  
  60.         exit(-1);  
  61.     }  
  62.     /*处理程序*/  
  63.     //从srcfile逐个复制字节到dstfile 效率有点低 不过先这么办吧/  
  64.     /* 
  65.      * 原理:当遇到/r时,检查下一个是否为/n 如果为/n 则 回退一个字节覆盖掉/r写入/n 
  66.      */  
  67.     flag = 0;  
  68.     while ((read=fread(buf,sizeof(char),1,in))>0)  
  69.     {  
  70.         if (buf[0] == '/r')  
  71.             flag = 1;  
  72.         else if (flag == 1 && buf[0]=='/n')  
  73.         {  
  74.             fseek(out,-1L,SEEK_CUR);    //从当前位置回退一个字节  
  75.             flag = 0;  
  76.         }  
  77.         fwrite(buf,sizeof(char),read,out);  
  78.     }  
  79.     fclose(in);  
  80.     fclose(out);  
  81. }  
  82. void turndos(char *srcfile, char *dstfile)  
  83. {  
  84.     FILE *in,*out;  
  85.     char buf[1];  
  86.     char ch = '/r';  
  87.     int read = 0;  
  88.     //以二进制形式打开文件   
  89.     if ((in=fopen(srcfile,"rb")) == NULL)  
  90.     {  
  91.         printf("/nCan not open file %s",srcfile);  
  92.         exit(-1);  
  93.     }  
  94.     //判断文件是否已存在   
  95.     if ((out=fopen(dstfile,"rb"))!=NULL)  
  96.     {  
  97.         fclose(out);  
  98.         printf("/nERROR! dstfile %s has been existed!",dstfile);  
  99.         exit(-1);  
  100.     }  
  101.     if ((out=fopen(dstfile,"wb"))==NULL)  
  102.     {  
  103.         printf("/nCan not create dstfile %s",dstfile);  
  104.         exit(-1);  
  105.     }  
  106.     /*处理程序*/  
  107.    /*遇到/n时,向文件中写入/r/n */  
  108.     while ((read=fread(buf,sizeof(char),1,in))>0)  
  109.     {  
  110.         if (buf[0] == '/n')  
  111.         {  
  112.             fwrite(&ch,sizeof(char),1,out);  
  113.         }  
  114.         fwrite(buf,sizeof(char),read,out);  
  115.     }  
  116.     fclose(in);  
  117.     fclose(out);  
  118. }  

原创粉丝点击