Hi all,
I am stuck here after several hours tried.
Here the thing. I have txt file that contain float data (1 column and many row) such like this (4 float data):
0.799
0.851
0.926
1.000
Then i want to read it as array. My code is work until this point.
After read it as array, than the code should copy each data
as much as number enter by user.
The user should enter 4 different number, since there are 4 float data in the read txt file.
For example if user enter number 2,3,1,2 respectively, the code must copy the 0.799 two times, the 0.851 three times, and so on.
Then put in the different txt file.
In this case the contain of new txt file must:
0.799
0.799
0.851
0.851
0.851
0.926
1.000
1.000
My coding failed to do this task, instead copying each of data as accumulation number input by user. So in this case the accumulative value from 2,3,1,2 is 8 (2+3+1+2).
My result so far are:
0.799
0.799
...
0.799 (until 8 row, then)
0.851
0.851
...
0.851 (until 8 row..and so on )
Here the code:
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>
main()
{
FILE *fileread,*fileprint;
int i,j, cp1, cp2, cp3, cp4;
double *z;
double wght[20],sic[20];
if ( ( fileread = fopen("datafile.txt", "r")) == NULL)
printf ("**** datafile could not be opened.\n");
if ((fileprint = fopen ("copiesdata.txt", "w+"))== NULL)
printf ("**** copiesdata could not be opened.\n");
printf("Number of copies for cases 1: ");
scanf("%i", &cp1);
printf("Number of copies for cases 2: ");
scanf("%i", &cp2);
printf("Number of copies for cases 3: ");
scanf("%i", &cp3);
printf("Number of copies for cases 4: ");
scanf("%i", &cp4);
i=0;
while (fscanf(fileread,"%s", wght) == 1)
{
z = (wght);
//printf("z = %s\n",z);
for (i=0, j = 0; j<cp1 ; j++)
{
sic[i] = atof(z);
fprintf(fileprint,"%.3f\n",sic[i]);
}
for (i=1, j = 0; j<cp2 ; j++)
{
sic[i] = atof(z);
fprintf(fileprint,"%.3f\n",sic[i]);
}
for (i=2, j = 0; j<cp3 ; j++)
{
sic[i] = atof(z);
fprintf(fileprint,"%.3f\n",sic[i]);
}
for (i=3, j = 0; j<cp4 ; j++)
{
sic[i] = atof(z);
fprintf(fileprint,"%.3f\n",sic[i]);
}
i=i+1;
}
fclose(fileread);
fclose(fileprint);
system("pause");
return(0);
}
Could you help me please.
Thank you in advanced.