I need help :/ this program is supposed to look for a text in a txt file, if this text is there, then generate another text. for example:
I generate randomly INSERT INTO ALUM_PROF VALUES (2,4); And I look in the txt if this text is found, if it is not then I write it in the txt.
Then it generate INSERT INTO ALUM_PROF VALUES (5,7); I look for it in the txt, if it is not, I write it in the txt
Then it generate INSERT INTO ALUM_PROF VALUES (2,4); I look for it in the txt, as it is in the, then I do not write it and I generate another one again.
int NUMEROS_AL_PROFE();
#define fila 100
int main()
{
char aux[200];
char aux2[200];
int contador=0;
FILE *f;
f = fopen("prueba.txt","a+");
if(f==NULL)
{
printf("no se ha podido abrir el archivo");
exit(1);
}
int i,num_prof,num_alum=1;
num_prof = NUMEROS_AL_PROFE();
fprintf(f,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n",num_alum,num_prof); //escribo en el fichero f
num_alum++;
for(i=0;i<fila;i++)
{
num_prof = NUMEROS_AL_PROFE();
sprintf(aux,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n",num_alum,num_prof); //almaceno el valor en aux
while(!feof(f))
{
fgets(aux2,200,f); //I read from the file f and I keep each line in aux2
if(strcmp(aux,aux2) == 0 ) //If a1 and a2 are equal then it is repeated.
{
contador=1;
}
memset(aux2, '\0',200); //Vacio el array aux2
}
memset(aux, '\0',200);
if(contador==0)
{
fprintf(f,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n",num_alum,num_prof);
}
num_alum++;
}
fclose(f);
}
//Random Number
int NUMEROS_AL_PROFE()
{
int num;
num = rand() % 17 + 1; //Numeros aleatorios entre 1 y 17
num = num + 1;
return num;
}
The program compiles, and when is running it remains loading, it simply does not write anything and generates a heavy txt .