转换linux到windows的文本文件,消除不可读小黑块

本程序用于消除linux下文本文件在windows下打开时的不可识别小黑块。
小黑块产生原因分析:
        在linux下,一些文本文件的一行(一般认为文件的一个回车换行)不一定以“/d/a“结束,有时候省去了 '/d' 字符,所以导致了文件到了windows下无法识别一行末尾字符,显示处小黑块,比如在windows下的记事本中。如果以“/d/a“结束,则在windows下不会产生小黑块,即可以识别。
        这里的解决方案就是将省去的'/d'字符补出来,于是就正常了。下面是代码部分,已经过了测试。

/*
    LtoW.cpp
    zhangggdlt
    2004.12.1
*/
#include <string.h>
#include <stdio.h>
#define MAX_LEN 1000

/*
 This function is used to exchange linux txt(cpp/c etc) to windows' files.
 So the unrecognized charactars can be recognized by exchange.
 
*/
int processFile(const char* sfile, const char* dfile)
{
 FILE *sfp = NULL;
 FILE *dfp = NULL;
 char* str = NULL;
 char  temp[MAX_LEN];
 int strLen = 0;
 if (!(sfp = fopen(sfile, "r")))
  return -1;
 if (!(dfp = fopen(dfile, "w")))
  return -2;
 while (str=fgets(temp, MAX_LEN, sfp))
 {
  //printf("%s", str);
  //show(str);
  strLen = strlen(str);
  if (*(str+strLen-2) != 0x0d)
  {
   *(str+strLen-1) = 0x0d;
   *(str+strLen) = 0x0a;
   *(str+strLen+1) = 0x00;
  }
  //printf("%s", str);
  //show(str);
  fputs(str, dfp);
 }
 
 fclose(sfp);
 fclose(dfp);
 
 return 0;
}

//test
int main()
{
 int err = processFile("main.cpp", "mm.cpp");
 printf("Error is: %d/n", err);
 
 return 0;
}
———————————————————————————————————————
                                                                                                                             zhangggdlt
                                                                                                                             2004.12.1
                                                                                                                                (完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值