Jump to content



Recommended Posts

Ola pessoal,

Queria fazer o seguinte: no inicio de um programa ao tentar abrir um ficheiro, se nao existir, cria-o. Se existir utiliza-o para leitura (sem o destruir/recriar)

Eu pensei que colocando a opcao "r+" no fopen a funçao fizesse o que aqui descrevo. Mas a função retorna NULL.

Apenas a "w+" cria de facto o ficheiro, mas essa, se o ficheiro ja existir recria-o e nao é isso que quero.


Link to comment
Share on other sites

Qual é o problema de fazer simplesmente:

FILE *file;

if((file = fopen("nome","r")) == NULL) 
  file = fopen("nome", "w");

Também podes usar fopen("nome", "a+") e depois um rewind(), mas é um pouco estranho.

❝The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying.❞- John Carmack on software patents

A list  of command line apps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.