I actually try to generate and fill random matrix and save it in plain text, txt, but have problems when I try to generate more than 1000 files than equivalent to 2000 files in working directory.
I want understand the reason and the solution for it.
I compile this code using gcc code.c
and run using ./a.out;
maybe change number of matrix to generate in line 25:
cant = 1000; // THIS GENERATE 1000 FILES OF CURRENT MATRIX.
code.c:
SOLUTION!!! =D
Thanks Chistopher and Vallabh
I think for me that error was an oversight on the line
close (puntero_f); close (puntero_b);
thank you very much for your explanations, I think I'll have them after even more clear concepts about files.
finally understood as post code, so I leave the solution for those who may be useful in the future
Thank you very much;)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
// Usando Preprocesado para manejar las matrices y tener acceso eficiente a memoria
#define F(i,j) F[i*col +j]
#define B(i,j) B[i*col +j]
float randfloat(float min, float max){
return ((float) rand() / (float) RAND_MAX);
}
int i, j, tstep, fil, col, cant;
int main(int argc, char *argv[]){
char nombre_b[50] ; //= "";
char nombre_f[50] ; //= "";
FILE *puntero_f;
FILE *puntero_b;
fil = 2;
col = 2;
cant = 1000;
int tam = fil*col;
float *F = calloc(tam,sizeof(float));
float *B = calloc(tam,sizeof(float));
//srand((unsigned)time(NULL));
int archi = 0;
for (tstep=0; tstep<cant; tstep++){
sprintf(nombre_f, "Forward%d.txt", tstep);
sprintf(nombre_b, "Backward%d.txt", tstep);
puntero_f = fopen(nombre_f, "a+");
puntero_b = fopen(nombre_b, "a+");
for(i = 0; i<fil; i++){
for(j = 0; j<col; j++){
F(i,j) = randfloat(0.0f, 1.0f);
B(i,j) = randfloat(0.0f, 1.0f);
if(tstep==archi){
fprintf(puntero_f,"%f ", F(i,j));
fprintf(puntero_b,"%f ", B(i,j));
}
}
if(tstep==archi){
fprintf(puntero_f,"\n");
fprintf(puntero_b,"\n");
}
}
archi++;
fclose(puntero_f);
fclose(puntero_b);
}
return 0;
}
at this point a question arises me, that is not generating more than 1000 files?
txt only generates up Foward999.txt? files?