0

In this piece of code I'm trying to check if a string is already in a list. Now I have to use the fread and fwrite functions to store the information in a binary file. I tried doing something but it doesn't seem to take me anywhere. This is the original code without the fread and fwrite:

if(cont < N)
{
    printf("\n");
    printf("Enter the club name: ");
    gets(club[cont].name);
    for(i = 0; i < cont; i++)
    {
        res_entered = strcmp(club[cont].name, club[i].name);
        if(res_entered == 0)
        {
            do
            {
                printf("This club is present in the list. Enter another one: \n");
                gets(club[cont].name);
                res_entered = strcmp(club[cont].name, club[i].name);
            }while(res_entered == 0);
        }
    }
    cont++;
}
else
{
    printf("Error. Maximum number of clubs \n");
}

Can someone help me to understand how to use fread and fwrite in this particular case, please?

Code with fread and fwrite

if(cont < N)
{
    fp = fopen("list.dat", "ab");
    printf("\n");
    printf("Enter the club name: ");
    gets(club[cont].name);
    strcpy(club[i].name, club[cont].name);
    club[i].points = 0;
    club[i].gscored = 0;
    club[i].gconceded = 0;
    fwrite(&club[i], sizeof(club[i]), 1, fp);
    fclose(fp);
    for(i = 0; i < cont; i++)
    {
        fp = fopen("list.dat", "rb");
        while( (fread(&club[i], sizeof(club[i]), 1, fp)) == 1)
        {
            cont++;
        }
        res_entered = strcmp(club[cont].name, club[i].name);
        if(res_entered == 0)
        {
            do
            {
                printf("This club is present in the list. Enter another one:
                gets(club[cont].name);
                res_entered = strcmp(club[cont].name, club[i].name);
            }while(res_entered == 0);
        }
    }
    fclose(fp);

}
else
{
    printf("Error. Maximum number of clubs \n");
}
  • 5
    [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/6865932) – anastaciu May 27 '20 at 20:39
  • If you need something that reads a text file line by line, then `fgets` is the proper function to use; `fread` will read a specific number of bytes but will not read the file line by line (unless you implement your own `myFGets` on basis of `fread`). I'd suggest to simply use `fgets` – Stephan Lechner May 27 '20 at 20:42
  • 3
    Please show the code that did not work. There is no `fread` or `fwrite` in the code posted. I presume you want to read/write a whole `struct`. – Weather Vane May 27 '20 at 20:42
  • It would be helpfull if posted the structure too. Ideally a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – anastaciu May 28 '20 at 09:15
  • Any of those variables for which you don't show the declarations can be the source of the problem, without a reproducilbe example of the code it's nearly impossible to know what's wrong. The only small problem that's apparent is regarding `strcpy(club[i].name, club[cont].name);` which will eventually overwrite the source but that's not likely to be the main issue. – anastaciu May 28 '20 at 09:23

0 Answers0