0

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 .

RicardoBarros
  • 143
  • 1
  • 8
  • 3
    Don't name your functions ALLCAPS. That is for macros and for constants only. – lost_in_the_source Aug 29 '17 at 01:15
  • https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – lost_in_the_source Aug 29 '17 at 01:16
  • Need `rewind(f);` or `fseek`. – BLUEPIXY Aug 29 '17 at 01:17
  • I tried with the link codes but it does not work either :( – RicardoBarros Aug 29 '17 at 01:48
  • Mixing English and {locale} in identifiers, texts, file names, and comments is bad practice. Either one, or the other. Since third-party functions *are* English, use that if you want consistent readability. – DevSolar Aug 29 '17 at 03:34
  • Note that your `NUMEROS_AL_PROFE()` function generates a value between 2 and 18. As you approach 1700 entries in the file, you will be less and less likely to actually write any new entry — all the combinations of `num_alum` and a random number between 2 and 18 will have been used. Note that [`while (!feof(file))` is always wrong!](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Aug 29 '17 at 03:46

1 Answers1

1

You need to reset the reading position of the file and contador flag.

example of fix code:

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

#define fila 100

int NUMEROS_AL_PROFE(void);

int main(void){
    char aux[200];
    char aux2[200];
    int contador=0;
    FILE *f  = fopen("prueba.txt","a+"); 

    if(f==NULL) {
        printf("no se ha podido abrir el archivo");
        exit(1);
    }

    int num_alum = 1;

    srand(time(NULL));//Change seed of random number
    for(int i = 0; i < fila; i++){
        sprintf(aux,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n", num_alum++, NUMEROS_AL_PROFE());

        rewind(f);//The file reading position is set to the beginning.
        contador = 0;//reset flag
        while(fgets(aux2, sizeof aux2, f)){//while(!feof(f)) has a problem. It has already been pointed out by stackptr's comment.
            if(strcmp(aux, aux2) == 0 ){
                //fprintf(stderr, "DEBUG:already exist %s", aux);
                contador = 1;
                break;
            }
        }
        if(contador == 0){
            //fprintf(stderr, "DEBUG:write %s", aux);
            fputs(aux, f);
        }
    }
    fclose(f);
}

int NUMEROS_AL_PROFE(void){
    int num;
    num = rand() % 17 + 1;
    num = num + 1; 
    return num;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • And the code doesn't need a positioning operation between the `fgets()` that returns a null pointer (indicating EOF) and the `fputs()` because that's explicitly allowed. Very often — this is one of the exceptions — you need a positioning operation (`rewind()`, `fseek()` etc) between each read and write (or write and read) operation on a file opened 'for update' (with `+` in the mode). – Jonathan Leffler Aug 29 '17 at 03:40
  • u r the best!!!!! thanks bro my error was here " while(fgets(aux2, sizeof aux2, f)){" and "rewind(f);" this last one I missed it entirely. – RicardoBarros Aug 29 '17 at 03:41