本程序用于消除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
(完)