oliveira1991 Posted December 7, 2009 at 05:52 PM Report Share #299096 Posted December 7, 2009 at 05:52 PM #include<stdio.h> #include<stdlib.h> int inserir() { char nome[50]; char categoria[35]; char op; FILE *a; a = fopen("dados.txt","w"); printf("Nome: "); scanf("%c",&op); gets(nome); printf("Categoria: "); scanf("%c",&op); gets(categoria); printf("deseja inserir mais dados?(s)ou(n)"); scanf("%c",&op); fclose(a); switch(op) { case 's' : case 'S' : inserir() ; break; case 'n' : case 'N' : system("PAUSE"); break; } } int pesquisar() { char ch; printf("livros inseridos :\n\n"); FILE *a; a = fopen("dados.txt" , "r"); ch=getc(a); while(ch!=EOF) { putchar(ch); ch=getc(a); } fclose(a); } int main() { char opcao; do { printf("-----------------------"); printf("\n\tBibioteca\t\n"); printf("\n\n\t\t 1) inserir Livros"); printf("\n\n\t\t 2) pesquisar Livros"); printf("\n\n\t\t 3) Sair"); printf("\n\n\n\t\t\tOpcao: "); scanf(" %c", &opcao); fflush(stdin); switch(opcao) { case'1': inserir(); break; case'2': pesquisar(); break; case'3': break; default: puts("opcao INVALIDA!!!"); } getchar(); } while(opcao!=4); } boas! este meu programa nao esta a criar os ficheiros alguem me pode ajudar? Live for ever Link to comment Share on other sites More sharing options...
edsousa Posted December 7, 2009 at 06:48 PM Report Share #299111 Posted December 7, 2009 at 06:48 PM acrescenta o seguinte código ao fopen("dados.txt","w") na função inserir: if(a==NULL){ perror("fopen"); } e depois vê qual o erro que é sai para o ecrã. Já agora, abre o ficheiro com o modo "a" em vez de "w", senão ele vai truncar o ficheiro a 0 bytes de cada vez que o abrir. Tharis Fan ClubMay Tharis bless you Link to comment Share on other sites More sharing options...
oliveira1991 Posted December 8, 2009 at 11:05 AM Author Report Share #299222 Posted December 8, 2009 at 11:05 AM nada n criar! Live for ever Link to comment Share on other sites More sharing options...
IRX773 Posted December 8, 2009 at 12:24 PM Report Share #299234 Posted December 8, 2009 at 12:24 PM Pelo que me pareceu tu não tens o programa a criar o ficheiro. Só tens "w" ou seja só vai escrever, cá para mim usa o "wt" Pareceu-me também que tens aí uns problemas pois na função inserir tu fazes um scanf para a variável "op" antes de receberes as strings. Não percebi muito bem essa parte mas acho estranho. Epá ... assim por alto com eu vi nao vi mais nada de irregular mas experimenta alterar o parâmetro do fopen como te disse e vê esses scanf's antes dos gets Link to comment Share on other sites More sharing options...
bosnia Posted December 10, 2009 at 10:11 PM Report Share #299724 Posted December 10, 2009 at 10:11 PM tentas antes o R+ Link to comment Share on other sites More sharing options...
Localhost Posted January 2, 2010 at 06:05 PM Report Share #303738 Posted January 2, 2010 at 06:05 PM Não vou acrescentar grande coisa ao tópico, apenas um alerta. Não uses a função gets, pois corres o perigo de ter um buffer overflow. here since 2009 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