I have a file numbers.dat containing about 300 numbers(floating point,negative positive)in column format. The objective is to first fill in numbers.dat with 300 numbers and then extract 100 numbers each time into another file say n1.dat. The second file n2.dat will have the next 100 numbers from numbers.dat and so on for 3 subsets of files obtained from number.dat. I am unable to understand how the location of the last read 100th number is taken into account so that the file read and fetching for the next block occurs after the previos fetched number.
Trying out the Solution provided by Gunner :
FILE *fp = fopen("numbers.dat","r");
FILE *outFile1,*outFile2,*outFile3;
int index=100;
char anum[100];
while( fscanf(fp,"%s",anum) == 1 )
{
if(index==100)
{
// select proper output file based on index.
fprintf(outFile1,"%s",anum);
index++; }
if(index >101)
{
fprintf(outFile2,"%s",anum);
index++; }
}
The problem is only one data is being written. What should be the correct process?
I'd write a program for that as
something like this
Continue to read from number.dat and write to the corresponding output file based on the index of current number read.
Sample code.