atorresleticia

contasbanco

Sep 15th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define n 2
  5.  
  6. struct conta {
  7.     char numero[5], nome[40];
  8.     float saldo;
  9. };
  10. typedef struct conta Cadastro;
  11.  
  12. void criar_conta (FILE *arquivo){
  13.    
  14.     arquivo = fopen ("contas.bin", "wb");
  15.     int i;
  16.     Cadastro cadastro;
  17.     if (arquivo==NULL){
  18.         printf ("Erro ao criar o arquivo.");
  19.     }
  20.  
  21.     printf ("\t\t==========CADASTRO CLIENTES BANCO LALALA==========\n\n\n");
  22.  
  23.     for (i=0; i<n; i++){
  24.         printf ("Cliente %d:\n", i+1);
  25.         printf ("\tNome: ");
  26.         scanf("%[^\n]s", cadastro.nome);
  27.         printf ("\tNumero da Conta: ");
  28.         scanf ("%s", cadastro.numero);
  29.         printf ("\tSaldo: ");
  30.         scanf ("%f", &cadastro.saldo);
  31.         printf ("\n\n");
  32.         fflush(stdin);
  33.         fwrite (&cadastro, sizeof(Cadastro), 1, arquivo);
  34.     }
  35.  
  36.     fclose(arquivo);
  37.    
  38.     arquivo = fopen ("contas.bin", "rb");
  39.     printf ("\n");
  40.    
  41.     fread(&cadastro, sizeof(Cadastro), 1, arquivo);
  42.     while (!feof(arquivo)){
  43.         printf ("Nome: %s\nNumero da Conta: %s\nSaldo: %.2f\n\n", cadastro.nome, cadastro.numero, cadastro.saldo);
  44.         fread(&cadastro, sizeof(Cadastro), 1, arquivo);
  45.     }  
  46.     fclose(arquivo);
  47. }
  48.  
  49. int main (){
  50.  
  51.     FILE *file;
  52.     criar_conta(file);
  53.    
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment