domingo, 8 de setembro de 2013

2013/2-PA-2013.09.06: Laboratório 2: Arquivos

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
  int c, n;
  time_t seconds;
  
  time(&seconds);
  srand((unsigned int) seconds);
  FILE * fp, * fp2;

  fp = fopen("dados.txt", "w");
  if (fp == NULL){
perror("ERRO: não foi possível abrir o arquivo para escrita.");
return EXIT_FAILURE;
  }

  printf("Escrevendo o arquivo . . .\n");
  int numNumbers = 1000000;
  for (c = 0; c < numNumbers; c++) {
    n = rand()%100 + 1;
    fprintf(fp, "%d\n", n);
  }
  fclose(fp);
  
  fp2 = fopen("dados.txt", "r");
  
  if (fp2 == NULL){
perror("ERRO: não foi possível abrir o arquivo para escrita.");
return EXIT_FAILURE;
  }
  printf("Lendo o arquivo . . .\n");
  for (c = 0; c < numNumbers; c++) {
    fscanf(fp2, "%d", &n);
    fprintf(stdout, "%d  ", n);
  }
  fclose(fp2);
  
   
  return EXIT_SUCCESS;
}

Nenhum comentário:

Postar um comentário