/*下面代码供参考*/
/*第一种方法*/
#include
void main()
{
FILE *in,*out;
char ch,file[20];
printf("Please Enter the file name:\n");
scanf("%s",file);
if((in = fopen(file,"r"))==NULL)
{
printf("cannot open file!\n");
exit(1);
}
if((out = fopen("temp","w"))==NULL)
{
printf("cannot open file!\n");
exit(1);
}
while(!feof(in))
{
ch = fgetc(in);
if(ch == '\n') ch = '$';
fputc(ch,out);
}
fclose(in);
fclose(out);
unlink(file);
rename("temp",file);
}
/*第二种方法*/
#include
void main()
{
FILE *in;
long i=0;
char ch,file[20],temp[1000];
printf("Please Enter the file name:\n");
scanf("%s",file);
if((in = fopen(file,"r+"))==NULL)
{
printf("cannot open file!\n");
exit(1);
}
while(!feof(in))
{
ch = fgetc(in);
if(ch == '\n') ch = '$';
temp[i++] = ch;
}
temp[i]= '\0';
rewind(in);
fputs(temp,in) ;
fclose(in);
}
最初想用fseek进行文件内部指针定位,直接读写一个文件,想更简单一点!但是这个函数一般用于二进制文件,对于文本文件会出现定位错误,所以只好另想办法了!
。
全部