主题:写文件出错了请指教
貌似这个程序没错,一旦将b写进文件,就不行了,请指教谢谢。
#include <stdio.h>
#include <string.h>
main()
{
char a[42];
char b[46];
char c[9]={"_swab.GDR"};
char line[80];
int i,j,k,q=0;
FILE *fp1, *fp2;
fp1=fopen("filepath.txt","r");
fp2=fopen("ssx.txt","w");
k=1;
while (!feof(fp1)){
fread(&a,sizeof(a),1,fp1);
for(i=0;i<42;i++){if(a[i]=='.')
{b[i]='\0';strcat(b,c);break;}
else b[i]=a[i];}
printf("%s %d\n",b,k);
k=k+1;
fwrite(&b,sizeof(b),1,fp2);
memset(b, 0, 46) ;
b[0]=' ';
}
}
filepath.txt的内容如下:
/home/xtq/ps/Alt/Geosat/data/1985_090.GDR
/home/xtq/ps/Alt/Geosat/data/1985_091.GDR
/home/xtq/ps/Alt/Geosat/data/1985_092.GDR
/home/xtq/ps/Alt/Geosat/data/1985_093.GDR
/home/xtq/ps/Alt/Geosat/data/1985_094.GDR
/home/xtq/ps/Alt/Geosat/data/1985_095.GDR
/home/xtq/ps/Alt/Geosat/data/1985_096.GDR
/home/xtq/ps/Alt/Geosat/data/1985_097.GDR
想要的效果是:
/home/xtq/ps/Alt/Geosat/data/1985_090_swab.GDR
....
/home/xtq/ps/Alt/Geosat/data/1985_097_swab.GDR
#include <stdio.h>
#include <string.h>
main()
{
char a[42];
char b[46];
char c[9]={"_swab.GDR"};
char line[80];
int i,j,k,q=0;
FILE *fp1, *fp2;
fp1=fopen("filepath.txt","r");
fp2=fopen("ssx.txt","w");
k=1;
while (!feof(fp1)){
fread(&a,sizeof(a),1,fp1);
for(i=0;i<42;i++){if(a[i]=='.')
{b[i]='\0';strcat(b,c);break;}
else b[i]=a[i];}
printf("%s %d\n",b,k);
k=k+1;
fwrite(&b,sizeof(b),1,fp2);
memset(b, 0, 46) ;
b[0]=' ';
}
}
filepath.txt的内容如下:
/home/xtq/ps/Alt/Geosat/data/1985_090.GDR
/home/xtq/ps/Alt/Geosat/data/1985_091.GDR
/home/xtq/ps/Alt/Geosat/data/1985_092.GDR
/home/xtq/ps/Alt/Geosat/data/1985_093.GDR
/home/xtq/ps/Alt/Geosat/data/1985_094.GDR
/home/xtq/ps/Alt/Geosat/data/1985_095.GDR
/home/xtq/ps/Alt/Geosat/data/1985_096.GDR
/home/xtq/ps/Alt/Geosat/data/1985_097.GDR
想要的效果是:
/home/xtq/ps/Alt/Geosat/data/1985_090_swab.GDR
....
/home/xtq/ps/Alt/Geosat/data/1985_097_swab.GDR