Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

Problem with fread on a array of strings in C

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!!!
", 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!!!
", 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!!!
", 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!!!
", 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!

question from:https://stackoverflow.com/questions/65871070/problem-with-fread-on-a-array-of-strings-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...