Advertisement
tonibiro

PP lab6 prob2

Nov 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int n;
  7.     FILE *f = fopen("numere_intregi.txt", "r");
  8.     FILE *bp = fopen("nr_pozitive.out", "wb+");
  9.     FILE *bn = fopen("nr_negative.out", "wb+");
  10.  
  11.     fscanf(f, "%d", &n);
  12.     int i;
  13.     for(i = 0; i < n; ++i)
  14.     {
  15.         int x;
  16.         fscanf(f, "%d", &x);
  17.         if(x >= 0)
  18.             fwrite(&x, sizeof(int), 1, bp);
  19.         else
  20.             fwrite(&x, sizeof(int), 1, bn);
  21.     }
  22.  
  23.     fflush(bn);
  24.     fflush(bp);
  25.     fseek(bp, 0L, SEEK_SET);
  26.     fseek(bn, 0L, SEEK_SET);
  27.  
  28.     printf("nr neg: ");
  29.     while(1)
  30.     {
  31.         int x;
  32.         fread(&x, sizeof(int), 1, bn);
  33.         if(feof(bn))
  34.             break;
  35.         printf("x: %d\n", x);
  36.     }
  37.  
  38.     printf("nr poz: ");
  39.     while(1)
  40.     {
  41.         int x;
  42.         fread(&x, sizeof(int), 1, bp);
  43.         if(feof(bp))
  44.             break;
  45.         printf("x: %d\n", x);
  46.     }
  47.  
  48.     fclose(f);
  49.     fclose(bp);
  50.     fclose(bn);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement