CODE:
这个或许可以满足你的要求
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define BUFSIZE 512
int main(int argc,char *argv[])
{
FILE *ifp,*ofp;
char buf[BUFSIZE];
int number; // record entry number
int i;
double *x,*y,*z;
if((ifp = fopen("diala.xyz","r+") == NULL) // input file handle
{
printf("Cannot open this file!\n"
exit(1);
}
if((ofp = fopen(argv[1],"w+") == NULL) // output filename obtained from first commandline parameter
{
printf("Cannot write to this file!\n"
exit(1);
}
fgets(buf,BUFSIZE,ifp); // read record entry number
number = atoi(buf);
// memory allocation for three arrays
x = (double *)malloc(sizeof(double) * number);
y = (double *)malloc(sizeof(double) * number);
z = (double *)malloc(sizeof(double) * number);
i = 0;
while(fgets(buf,BUFSIZE,ifp) != NULL) // read info line by line
{
sscanf(buf,"%*d%*s%lf%lf%lf",&x[i],&y[i],&z[i]);
fprintf(ofp,"%lf %lf %lf\n",x[i],y[i],z[i]); // write into output file
i++;
}
fclose(ifp);
fclose(ofp);
// release memory
free(x);
free(y);
free(z);
return 0;
}