-
1 Attachment(s)
Trouble with Output .txt
Hello everyone!
This is my first post, so take it easy... hehe
I'm a begginer in programming, started few weeks ago, and I choose C as the first language to learn since I faced this task at while studying. So, let me explain what's going on... Well, I have a .txt file that contains, together with a few characters, columns of values that I want to save in different files like is written in the program (file 1, file2, file3 - a with x, b with y, c with z).
The pattern of the source text file is like this:
known_vector frame1 151 65 0 frame2 151.000763 64.538582 0.563737
known_vector frame1 152 65 0 frame2 152.000702 64.542488 0.560822
known_vector frame1 153 65 0 frame2 153.000671 64.546150 0.558089
.
.
.
Until now I could manage to split the files, but the output gives me only zeros.
First the program count the number of lines of the read text file, then it should display the desired columns of double values in three other .txt files.
I've got for the three .txt files columns like this:
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
.
.
.
Could someone help me??? Probably it is an easy to solve mistake, once everything is new for me...
Attachment 12471
Cheers!!! ;)
-
First, it's generally better to inline your code in code tags, like so.
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++;
When you've reached the end of the file, then all following fgets/fscanf calls will fail - unless you do something.
The something being
rewind(fp);
-
Thanks, Salem! It worked just fine.