0

I have a problem when passing a string to a file. I am almost sure that the problem comes from the way I read the variable, and so I leave the piece of code that I think is wrong. If you don't think the problem could come from there, I reassess the situation and send what I think is necessary.

I don't understand my problem because the system is actually saving something, but for example when I enter 5 teams, the system only registers 2 or 3 teams in the file. Does anyone know what this is about?

Here is the code:

My struct:


typedef struct String {

    char string[50];
    
}string; 

My variables:


int nEquipas = 0;
    
string equipa[50];

Here where I define the file location


#define FICHEIRO_EQUIPAS "Equipas.dat"
#define FICHEIRO_NEQUIPAS "nEquipas.dat"

Here when I ask the name of the n team:


void newTeam(string *equipa, int *nEquipas)
{
    
    puts("INSIRA O NOME DA EQUIPA: ");
    fflush(stdin);
    gets(equipa[*nEquipas].string); 
    system("cls");
    
    saveTeams(equipa, nEquipas);
    saveNumTeams(nEquipas);
}

Here when I write the variable nEquipas:

void saveNumTeams(int *nEquipa)
{
    
    // variaveis
    FILE *ficheiro;
    int i;
    
    
    ficheiro = fopen(FICHEIRO_NEQUIPAS, "wb");
    
    // se não foi possivel abrir o ficheiro (por exemplo: problema de permissões), mostra erro e sai
    if (ficheiro == NULL)
    {
        printf("!!!não foi possivel abrir o ficheiro %s!!!\n", FICHEIRO_NEQUIPAS);
        return;
    }
        
        
    //Escrever no ficheiro
    fwrite(nEquipa, sizeof(int), 1, ficheiro);

    
    // fechar o ficheiro
    fclose(ficheiro);
    
}

Here I save the array string equipas


void saveTeams(string *equipa, int *nEquipa)
{
    
    // variaveis
    FILE *ficheiro;
    int i;
    
    
    ficheiro = fopen(FICHEIRO_EQUIPAS, "a+b");
    
    // se não foi possivel abrir o ficheiro (por exemplo: problema de permissões), mostra erro e sai
    if (ficheiro == NULL)
    {
        printf("!!!não foi possivel abrir o ficheiro %s!!!\n", FICHEIRO_EQUIPAS);
        return;
    }
    
    
    //Procurar o fim
    fseek(ficheiro, 0, SEEK_END);
        
        
    //Escrever no ficheiro
    fwrite(equipa, sizeof(string), *nEquipa, ficheiro);
        
    // fechar o ficheiro
    fclose(ficheiro);
    
}

And here I read the two variables:


void gerirFicheiros(string *equipa, int *nEquipas, membro medico[][50], int *nMedicos)
{

    // variaveis
    FILE *ficheiro;
    int i;

    // tentar abrir ficheiro (r = leitura b = binario)
    ficheiro = fopen(FICHEIRO_NEQUIPAS, "rb");

    // se não foi possivel abrir o ficheiro (por exemplo: por não existir), mostra erro e sai
    if (ficheiro == NULL)
    {
        printf("!!!não foi possivel abrir o ficheiro %s!!!\n", FICHEIRO_NEQUIPAS);
        return;
    }

    fread(nEquipas, sizeof(int), 1, ficheiro);

    // fechar o ficheiro
    fclose(ficheiro);

    ficheiro = fopen(FICHEIRO_EQUIPAS, "rb");
    
    // se não foi possivel abrir o ficheiro (por exemplo: por não existir), mostra erro e sai
    if (ficheiro == NULL)
    {
        printf("!!!não foi possivel abrir o ficheiro %s!!!\n", FICHEIRO_EQUIPAS);
        return;
    }
    
    // posicionar no inicio do ficheiro
    rewind(ficheiro);  

    
     fread(equipa, sizeof(string), *nEquipas, ficheiro);
    

    // fechar o ficheiro
    fclose(ficheiro);

}

Am I reading correctly?

Thank you very much to everyone!

2
  • fflush(stdin) is undefined behaviour. Commented Jan 24, 2021 at 13:40
  • Are you trying to tell me that i should put a fflush (stdin) before the fwrite in the string? Commented Jan 24, 2021 at 14:56

2 Answers 2

1

In this loop:

for (i = 0; i <= *nEquipas; i++) {
    fread(&equipa[i], sizeof(string), (i + 1), ficheiro);
}

There are 2 issues. First, indexes into arrays go from 0 to array_size - 1. Second, the third parameter to fread is how many items to read, so in this case it should be 1.

for (i = 0; i < *nEquipas; i++) {
    fread(&equipa[i], sizeof(string), 1, ficheiro);
}

You could lose the loop altogether since you know the number of items:

fread(equipa, sizeof(string), *nEquipas, ficheiro);

Note that fread returns the number of items read. You should compare that against what was expected to check for error.

Another issue is you open the file for appending, but then write the entire list. This just keeps adding the list with 1 string, 2 string, 3 string etc.

You can either, not open for appending and write the entire list:

ficheiro = fopen(FICHEIRO_EQUIPAS, "wb");
fwrite(equipa, sizeof(string), *nEquipa, ficheiro);

Or, open for appending and just write the last item in the list:

ficheiro = fopen(FICHEIRO_EQUIPAS, "a+b");
fwrite(&equipa[*nEquipa-1], sizeof(string), 1, ficheiro);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the help, I changed the for and now I do the fread the way you proposed, however the problem turns out to be the same. Also in fwrite I replaced it in an identical way to yours. But when I introduce 2 teams, I only keep the first one.
@HugoOliveira in newTeam() shouldn't you be increasing the count? *nEquipas += 1.
I'm sorry, it was my fault, I didn't explain that part. Before my neyteam (), I have a menu before inside a do while and when I present a new team, my * nEquipas adds 1 value.
Also, when saving the teams you do not want to open for appending, since you write all the teams. Change to ficheiro = fopen(FICHEIRO_EQUIPAS, "wb");. OR if you want to append, just write the last team: fwrite(&equipa[*nEquipa-1], sizeof(string), 1, ficheiro);
I solved my problem, thanks for your help!
1

I already solve my problem. Here is the correction:

I change my fwrite:

Before:


    fwrite(equipa, sizeof(string), *nEquipa, ficheiro);
        

After :


fwrite(&equipa[*nEquipa], sizeof(string), 1, ficheiro);

I change my fread of equipa too:

Before:


     fread(equipa, sizeof(string), *nEquipas, ficheiro);

After


fread(&equipa[i], sizeof(string), 1, ficheiro);

Thank you very much for your help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.