|
楼主 |
发表于 2002-10-31 10:07:44
|
显示全部楼层
问题已经解决。
我编写了一个解决此问题的程序,可以转换两种格式的文件。由于使用的是标准的c函数,所以它是广泛适用的。有问题告诉我。
#include"stdio.h"
#include"string.h"
void usage() /* show the usage of the program */
{
printf("\n Usage\t\t ctxfm -u|-U|-d|-D infile outfile\n\n");
printf(" -u -U\tturn a dos text into unix form.\n");
printf(" -d -D\tturn a dos text into unix form.\n\n");
exit(0);
}
/* turn a dos text into unix form */
void turnunix(char *infile,char *outfile)
{FILE *in,*out;
char ch,flag=0;
if((in=fopen(infile,"rb"))==NULL) /* open infile */
{printf("Cannot open file %s\n",infile);
exit(0);
}
if((out=fopen(outfile,"rb"))!=NULL) /* open outfile */
{fclose(out);
printf("ERROR! Outfile %s has been existened!",outfile);
exit(0);}
if((out=fopen(outfile,"wb"))==NULL)
{printf("Cannot open outfile %s\n",outfile);
exit(0);
}
while(1) /* write the outfile */
{ch=fgetc(in);
if(feof(in))break;
if(ch=='\r')flag=1; /*in dos is \r\n*/
else if((ch=='\n')&&flag)fseek(out,-1L,1);
else flag=0;
fputc(ch,out);
}
if(fclose(in)==EOF||fclose(out)==EOF) /* close the file */
{printf("File close error!\n");
exit(0);
}
}
/* turn a unix text form into dos form */
void turndos(char *infile,char *outfile)
{FILE *in,*out;
char ch;
if((in=fopen(infile,"rb"))==NULL) /* open infile */
{printf("Cannot open file %s\n",infile);
exit(0);
}
if((out=fopen(outfile,"rb"))!=NULL) /* open outfile */
{fclose(out);
printf("ERROR! Outfile %s has been existened!",outfile);
exit(0);}
if((out=fopen(outfile,"wb"))==NULL)
{printf("Cannot open outfile %s\n",outfile);
exit(0);
}
while(1) /* write the outfile */
{ch=fgetc(in);
if(feof(in))break;
if(ch=='\n') /* in unix is \n */
fputs("\r\n",out);
else fputc(ch,out);
}
if(fclose(in)==EOF||fclose(out)==EOF) /* close the file */
{printf("File close error!\n");
exit(0);
}
}
void main(int argc,char *argv[])
{
/* check parameters */
if(argc!=4)usage();
/*deal with different cases*/
if(!strcmp(argv[1],"-u")||!strcmp(argv[1],"-U"))
turnunix(argv[2],argv[3]);
else if(!strcmp(argv[1],"-d")||!strcmp(argv[1],"-D"))
turndos(argv[2],argv[3]);
else usage();
} |
|