Is it a problem for me to work only with reading a file with it open in both write and read mode?

Asked

Viewed 55 times

0

I’m starting to work with C files and need to do the skeleton of a book registration program, saving the information in a file. My question is whether I should work with the files like this, opening the file at the beginning of the program and closing at the end:

int main(){     
    int aux=0, menu;
    FILE *acervo;

    acervo = fopen("acervo.txt", "a+");    

    do {
        system("cls");
        printf ("O que você quer fazer? \n 1 – Inserir um novo cadastro \n 2 – Mostrar todos os cadastros \n 0 – Encerrar \n");
        scanf ("%d", &menu);

        if( menu == 1){
            cadastroLivro();                
        } else if (menu == 2){              
            printf("Aqui mostra os dados cadastrados (não está pronto). \n");                                   
        } else if(menu != 0) {          
            printf("Erro: opção inválida! \n");     
        }           
    } while (menu != 0);        
    fclose(acervo);     
}

Or so, opening and closing each task:

int main(){     
    int aux=0, menu;
    FILE *acervo;

    do {
        system("cls");
        printf ("O que você quer fazer? \n 1 – Inserir um novo cadastro \n 2 – Mostrar todos os cadastros \n 0 – Encerrar \n");
        scanf ("%d", &menu);

        if( menu == 1){
            acervo = fopen("acervo.txt", "a");
            cadastroLivro();
            fclose(acervo);    
        } else if (menu == 2){
            acervo = fopen("acervo.txt", "r");
            printf("Aqui mostra os dados cadastrados (não está pronto). \n");
            fclose(acervo);    
        } else if(menu != 0) {          
            printf("Erro: opção inválida! \n");   
        }    
    } while (menu != 0);
}

I understand that the first option leaves a bigger margin for the file to corrupt, especially when I just want to read it and it is allowing writing, but the second one makes the "program" slow down, so I don’t know what the best practice here.

  • In all this, it depends, without context you can’t say.

  • I tried to give the context.. what was missing?

  • Where is the context?

  • 1

    The idea of your program is that it starts at the beginning of the day and stays running until night or is executed only when it is necessary and closed as soon as the option is executed? The answer will determine whether it is more advisable to open once and close at the end or to open and close each option chosen. I believe that your program will be slower with only an extremely large number of file openings and closures, I do not think it is anything significant with few thousand operations.

  • It’s just a job, to be open only when the teacher runs the xD code but if I can put it into the thousands then I think opening and closing each action would be a better practice in this case, since even if I imagine a situation where this program would be effectively used, would not reach the house of a thousand requisitions in one day...

  • @Thavilang The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how to do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

Show 1 more comment

1 answer

1

These decisions depend on the real context. I know everybody wants a magic decision that works for everything, but it can’t be like this, if it wasn’t I wouldn’t need programmers, all problems would have been solved by now.

For this case either makes the choice it makes because it is an exercise that has no real requirements on it. In other cases every detail of the requirement can make a difference. That is why I always say that artificial requirements are not good to help learn how to solve problems, they serve only to force certain specific mechanisms to be used, and not in the right way, but only that you are legally right from the point of view of language.

Although anyone serves the purpose very well, because it is an exercise and very simple I prefer the code that does not repeat, so the first one looks much better. To tell the truth I have no idea why did the second, it can be interesting in some very specific case, that is not the presented, and the specific case probably could not do different.

Seeing this code I care more about other things than this.

  • If you point out the other problems you see I’d be happy too.. :)

  • And about why I did the second one, I understand that in this particular case it doesn’t have much relevance, but I was trying to think of a real situation, which would be the right one.

  • I answered your question, the mistakes are not serious. As I said, a situation has real specifics that must be considered, in case there is nothing real, only a possible speculation that only you know, so I didn’t even want to answer the question, I can only answer what was asked, not what might be good in another real case that no one knows. A real case this code would be absurdly different, everything you learn in C is not like a real program in real projects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.