Advertisement
luistavares

Lendo o conteúdo de um vetor gravado em arquivo

Oct 25th, 2012
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include<conio.h>
  2. #include<stdio.h>
  3. int main(void) {
  4.     FILE *f;
  5.     int i, vet[10];
  6.  
  7.     /* Abre o arquivo para leitura em modo binário. */
  8.     f = fopen("arquivo.bin", "rb");
  9.     if (f == NULL ) {
  10.         printf("Erro ao abrir o arquivo! \n");
  11.         getch();
  12.         return 0;
  13.     }
  14.  
  15.     /* Lê o conteudo do vetor do arquivo */
  16.     fread(vet, sizeof(int), 10, f);
  17.  
  18.     /* Apresenta os números do vetor. */
  19.     for (i = 0; i <= 9; i++) {
  20.         printf("Elemento %d, Valor: %d \n", i + 1, vet[i]);
  21.     }
  22.  
  23.     /* Fecha o arquivo */
  24.     if (fclose(f) != 0) {
  25.         printf("Erro ao fechar o arquivo");
  26.     }
  27.     getch();
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement