C Search and write to a txt file

2

I need help: / this program is supposed to look for a text in a txt file, if this text is there, then it generates another text and if that is not it writes it. for example:

Aleatoreamente gender 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 generates 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 generates INSERT INTO ALUM_PROF VALUES (2,4); I look for it in the txt, as it is in it, so I do not write it and I generate another one again. and so

#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, '
#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, '%pre%',200);  //Vacio el array aux2
        }
        memset(aux, '%pre%',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;
}
',200); //Vacio el array aux2 } memset(aux, '%pre%',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 executing it stays stuck and in the txt it does not write anything at first sight, but it fills up since it can weigh up to 1 gb, I have no idea what it is writing: l

    
asked by EmiliOrtega 29.08.2017 в 04:11
source

1 answer

1

You are opening the file in advanced editing mode ...

FILE *f;
f = fopen("prueba.txt","a+"); 

Then you start writing in the file:

num_prof = NUMEROS_AL_PROFE();
fprintf(f,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n",num_alum,num_prof); //escribo en el fichero f

And suddenly it gives you to search the file:

while(!feof(f))

What do you hope to find there? You are writing in the file then the pointer will be at the end of the file ... that if you do not end up having problems with the stream cache ...

What you have to do is position yourself at the beginning of the file before each search ... and of course guarantee that you position yourself at the end of the file before each writing. Although it may seem trivial to do this, you have to consider certain considerations :

  

Notes

     

After searching for a non-end position in a wide stream, the next call to any output function may render the remainder of the file undefined, e.g. by outputting a multibyte sequence of a different length.

     

For text streams, the only valid values of offset are 0 (applicable to any origin) and a value returned by an earlier call to ftell (only applicable to SEEK_SET).

     

POSIX allows seeking beyond the existing end of file. If an output is performed after this seek, any read from the gap will return zero bytes. Where supported by the filesystem, this creates a sparse file.

     

POSIX also requires that fseek first performs fflush if there are any unwritten data (but if the shift state is restored is implementation-defined).

Summing up:

  • When working with non-ASCII files, writing to random positions in the file can result in undefined behavior. That is, you can end up bothering the coding of the file).

  • When working in text mode the only valid offset is 0. The only exception is the values returned by ftell (and in this case you must use SEEK_SET )

  • EYE to this : POSIX supports the search for elements beyond the final file. Of course, if you happen to write past that limit the searches in the intermediate regions will not get results.

  • fseek requires you to empty the buffer of the file. (This point may be important in explaining your problem.)

So your algorithm should perhaps look something like this:

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

    fflush(f);
    int pos = ftell(f);
    fseek(f,0,SEEK_SET);
    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;
            break; // No hace falta seguir buscando, cierto?
        }
        // memset(aux2, '
if(contador==0)
{
    fprintf(f,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n",num_alum,num_prof);
}
',200); --> no es necesario } // memset(aux, '
if(contador==0)
{
    fprintf(f,"%s",aux);
}
',200); --> no es necesario fseek(f,pos,SEEK_SET); if(contador==0) { fprintf(f,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n",num_alum,num_prof); }

2nd revision

I missed a little detail: the variable contador is not reset never . Consequently, as soon as the algorithm finds the first duplicate line it will stop adding new lines. You should reset the value in each iteration (one possibility):

Another strange detail that I see the program is that always is going to insert a line by execution, the first line is inserted always regardless of whether it already exists.

And, finally, this:

FILE *f;
f = fopen("prueba.txt","a+"); 

It can be replaced by this:

num_prof = NUMEROS_AL_PROFE();
fprintf(f,"INSERT INTO ALUM_PROF VALUES (%d,%d);\n",num_alum,num_prof); //escribo en el fichero f

I think it does not deserve explanation, if the string to be stored in the file is already generated and stored in aux I see no need to rebuild it.

The program works for me correctly. If it still does not work, or you're copying something wrong or the libraries you use are defective.

    
answered by 29.08.2017 / 09:14
source