Code:
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
int i, count;
double *a, *b, *c;
double *x, *y, *z;
char tag[5][255];
int main(int argc, char *argv[])
{
char line[255];
FILE *fp, *file1, *file2, *file3;
long NumberOfLines = 0;
if (argc != 5)
{
printf ("Usage: %s filename newfilename1 newfilename2 newfilename3 \n", argv[0]);
exit(1);
}
if ((fp = fopen(argv[1], "r")) == NULL)
{
printf("Can't open %s\n", argv[1]);
exit(1);
}
while( fgets(line,sizeof(line),fp) != NULL) NumberOfLines++;
fgets(tag[0], 255, fp);
fgets(tag[1], 255, fp);
count = NumberOfLines;
a=calloc(count, sizeof(double));
b=calloc(count, sizeof(double));
c=calloc(count, sizeof(double));
x=calloc(count, sizeof(double));
y=calloc(count, sizeof(double));
z=calloc(count, sizeof(double));
if ( (file1 = fopen(argv[2], "w" )) == NULL )
{
printf ("\n Cannot read the new file \n");
}
if ( (file2 = fopen(argv[3], "w" )) == NULL )
{
printf ("\n Cannot read the new file \n");
}
if ( (file3 = fopen(argv[4], "w" )) == NULL )
{
printf ("\n Cannot read the new file \n");
}
for ( i=0; i<count; i++ )
{
fscanf (fp,"%s %s %lf %lf %lf %s %lf %lf %lf", tag[0], tag[1], &a[i], &b[i], &c[i], tag[2], &x[i], &y[i], &z[i]);
// The pattern in the read file was like this:
// "known_vector_chest_surface frame1 151 65 0 frame2 151.000763 64.538582 0.563737"
// tag[0] tag[1] &a[i] &b[i] &c[i] tag[2] &x[i] &y[i] &z[i]
fprintf (file1, "%lf \t\t %lf \n", a[i], x[i]);
fprintf (file2, "%lf \t\t %lf \n", b[i], y[i]);
fprintf (file3, "%lf \t\t %lf \n", c[i], z[i]);
}
free(a);
free(b);
free(c);
free(x);
free(y);
free(z);
return 0;
}
> while( fgets(line,sizeof(line),fp) != NULL) NumberOfLines++;