Necesito ayuda: / este programa se supone que busca un texto en un archivo txt, si este texto está allí, entonces genera otro texto y si ese no está lo escribe. por ejemplo:
Genero aleatoreamente INSERT INTO ALUM_PROF VALUES (2,4); Y miro en el txt si se encuentra este texto, si no está entonces lo escribo en el txt.
luego genera INSERT INTO ALUM_PROF VALUES (5,7); Lo busco en el txt, si no está, lo escribo en el txt
después genera INSERT INTO ALUM_PROF VALUES (2,4); Lo busco en el txt, como está en el, entonces no lo escribo y genero otro de nuevo. y así
#include <stdlib.h>
#include <string.h> //strlen
#include <stdio.h>
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;
}
El programa compila y al ejecutar se queda pegado y en el txt no escribe nada a simple vista, pero se llena ya que puede pesar hasta 1 gb, no tengo idea lo que está escribiendo :l