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.

You really should be able to do this with one while loop and one for loop. The while loop reads the file and the for loop takes care of the number of entries.

I tried playing with your code and came up with this, its hard coded to a four digit code - for example when prompted the user enters a 4 digit number code, say 3267 which will print the first entry 3 times and the second entry 2 times and the third entry 6 times and finally the fourth entry 7 times...Now this piece of code is very limited in that each entry can only have the values 0 - 9, If you try a two digit value then the code breaks down...

It a nice demo on how to use the while to read the file and the for loop to handle how many times to write

#include <stdio.h>
#include <stdlib.h>

int main() 
{
	FILE *fileread,*fileprint;
	char filestr[10];
	int tnum = 0;
	int multi = 1000;
	int i = 0;

	if (!( fileread = fopen("datafile.txt", "r")))
	{
		printf ("**** datafile could not be opened.\n");
		exit(EXIT_FAILURE);
	}

	if (!(fileprint = fopen ("copiesdata.txt", "w+")))
	{
		printf ("**** copiesdata could not be opened.\n");
		exit(EXIT_FAILURE);
	}


	fputs("enter the 4 digit number code->", stdout);
	fscanf(stdin, "%d", &tnum);
	

	while ((fscanf(fileread, "%s", &filestr[0])) != EOF)
	{
		for (i = 0; i < (tnum - (tnum % multi))/multi; ++i)
			fprintf(fileprint, "%s\n",filestr);

		tnum -= (tnum - (tnum % multi));
		multi /= 10;
	}
	
	fclose(fileread);
	fclose(fileprint);
	return(0);
}
commented: nice alternative +2

Thank you for the nice code. It's almost solved the problem i present here.
I appreciate it. However, my original problem is more complicated.
One of them is that the user should able enter more than one digit value.

Another thing is, the number float data actually is dynamic, not just 4 float data as i present here, sometimes more than 100.
I realized, such condition will need "malloc" or "calloc".

Since the number of float data is dynamic, the user in particular condition will enter more than 4 number.

Looks a like every point i mention related to dynamic condition. Pheww..such hard problem for me.

I will try to adopt your code in my problem. Further suggestion and help are very welcome.

regards,

me

Thank you for the nice code. It's almost solved the problem i present here.
I appreciate it. However, my original problem is more complicated.
One of them is that the user should able enter more than one digit value.

Another thing is, the number float data actually is dynamic, not just 4 float data as i present here, sometimes more than 100.
I realized, such condition will need "malloc" or "calloc".

Since the number of float data is dynamic, the user in particular condition will enter more than 4 number.

Looks a like every point i mention related to dynamic condition. Pheww..such hard problem for me.

I will try to adopt your code in my problem. Further suggestion and help are very welcome.

regards,

me

If you require a dynamic solution, then consider an integer array to hold your digit values..

int number;

int *myarray = (int*)malloc(number * sizeof(int));

myarray[0] = first value
myarray[1] = second value
myarray[2] = third value
myarray[3] = fourth value
.
.
.
myarray[number -  1] = last value

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 )

My guess is you're supposed to enter as many numbers as in the file, right? So after you read the file, set up another loop and input the numbers into another array. Now your float value [1] will correspond to entered integer [1].

Now, set up a loop to process each float.
Inside that loop, another loop to process the corresponding integer value.

That should do it.


And PLEASE fix your Formatting -- it's terrible.

commented: thanks WaltP finally i did it... +2

If you require a dynamic solution, then consider an integer array to hold your digit values..

So far for generating the dynamic float number i used this code:

...
      int number; //represent numbers in the file
      float *si;
      ...
      si = malloc (number*sizeof(float));
      ...

it works. i might be wrong used this approach, since i not considered the dynamic number enter by user.
Thank for your suggestion gerard4143.

My guess is you're supposed to enter as many numbers as in the file, right? So after you read the file, set up another loop and input the numbers into another array. Now your float value [1] will correspond to entered integer [1].

Now, set up a loop to process each float.
Inside that loop, another loop to process the corresponding integer value.

That should do it.

And PLEASE fix your Formatting -- it's terrible.

You are right, the user should enter the number as many as number in the file.
Thank for the suggestion, kind hard to catch up for a novice like me. I think this novice situation, explained a lot my formatting terrible, i will learn to fix it. Thank WaltP :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.