Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct
- {
- char nome[20];
- }Contato;
- void inserir();
- void listar();
- int main()
- {
- int opcao;
- do
- {
- printf("1 - Inserir\n");
- printf("2 - Listar\n");
- printf("Digite: ");
- scanf("%d", &opcao);
- switch(opcao)
- {
- case 1:
- inserir();
- break;
- case 2:
- listar();
- break;
- default:
- printf("Opcao invalida\n");
- }
- }while(opcao != 3);
- }
- void inserir()
- {
- FILE *arquivo;
- Contato add;
- if((arquivo = fopen("contato.dat", "ab"))== NULL)
- {
- printf("erro ao abrir o arquivo\n");
- exit(1);
- }
- else
- {
- printf("Nome: ");
- scanf("%s", add.nome);
- fwrite(&add,sizeof(Contato),1,arquivo);
- printf("Nome adicionado com sucess\n");
- }
- fclose(arquivo);
- }
- void listar()
- {
- FILE *arquivo;
- Contato add;
- if((arquivo = fopen("contato.dat","rb"))==NULL)
- {
- printf("Houve um erro ao abrir este arquivo\n\n");
- exit(1);
- }
- else
- {
- while(fread(&add,sizeof(Contato),1,arquivo)==1)
- {
- printf("Nome: %s\n", add.nome);
- }
- }
- fclose(arquivo);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement