我在C中有下列程序,用於將UNIX文本文件轉換爲Windows格式(LF-> CR LF)。基本上使用目的是addcr infile > outfile
在命令行:在C打印垃圾中添加回車工具?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fp;
char *buffer;
int i, flen;
if(argc<2)
{
printf("Usage: addcr filename\n");
return 0;
}
fp=fopen(argv[1], "r");
if(fp==NULL)
{
printf("Couldn't open %s.\n", argv[1]);
return 0;
}
fseek(fp, 0, SEEK_END);
flen=ftell(fp);
rewind(fp);
buffer=(char*)malloc(flen+1);
fread(buffer, 1, flen, fp);
fclose(fp);
buffer[flen]=0;
for(i=0;i < strlen(buffer);i++)
{
if(buffer[i]==0x10)
{
printf("%c", '\r');
}
printf("%c", buffer[i]);
}
free(buffer);
return 0;
}
然而,有時它打印出垃圾在文件內容的結束時,通過它的輸出進行比較來TYPE命令所指示的:
C:\Temp>addcr sample.txt
He did not wear his scarlet coat,
For blood and wine are red,
And blood and wine were on his hands
When they found him with the dead,
The poor dead woman whom he loved,
And murdered in her bed.
Window
C:\Temp>type sample.txt
He did not wear his scarlet coat,
For blood and wine are red,
And blood and wine were on his hands
When they found him with the dead,
The poor dead woman whom he loved,
And murdered in her bed.
C:\Temp>
它似乎有時在我的環境變量中打印出一些不可預知的字符串部分。我完全不知道可能導致它的原因。有誰知道如何解決這個問題?
謝謝! :)現在得到它的工作。 –