msr Posted December 6, 2009 at 03:47 AM Report Share #298888 Posted December 6, 2009 at 03:47 AM 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. Sugestões? COMPONENT ORGANIZER easily organize your electronic parts and application notes Link to comment Share on other sites More sharing options...
IceBrain Posted December 6, 2009 at 04:13 AM Report Share #298890 Posted December 6, 2009 at 04:13 AM 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 More sharing options...
msr Posted December 6, 2009 at 04:17 AM Author Report Share #298891 Posted December 6, 2009 at 04:17 AM Exactamente, foi isso que acabei por por mas com o "w+" no segundo fopen() uma vez que queria isto para leitura. Eu tava mesmo na ideia que o "r+" fazia isso... Obrigado IceBrain. COMPONENT ORGANIZER easily organize your electronic parts and application notes Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now