Advertisement
nicb

ESERCIZIO 2 - SLIDE 4

Mar 17th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. ///BISOGNA CREARE IL FILE "testfile.txt" ALL'INTERNO DELLA CARTELLA DOVE È PRESENTE IL "main.c"
  2.  
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. int main()
  10. {
  11.     ///Dichiaro il buffer
  12.     char buffer[2];
  13.     char buffer_o[2];
  14.     ///Dichiaro il descrittore
  15.     int fp_i,fp_o,fp_i_2,fp_o_2;
  16.     ///Dichiato i due interi e la var di somma
  17.     int n1,n2,somma;
  18.     ///Apro il descrittore
  19.     fp_i=open("testfile.txt",O_RDONLY , 0700);
  20.     fp_i_2=dup(fp_i);
  21.     fp_o=open("outputfile.txt",O_CREAT | O_WRONLY | O_TRUNC, 0700);
  22.     fp_o_2=dup(fp_o);
  23.     ///Procedura di input
  24.     while((read(fp_i_2,buffer,2))>0)
  25.     {
  26.         if((n1=atoi(buffer))==-1) return 0;
  27.         if((read(fp_i_2,buffer,2))>0)
  28.             {
  29.                 n2=atoi(buffer);
  30.                 somma=n1+n2;
  31.                 sprintf(buffer_o, "%d" , somma);
  32.                 if(somma>9)
  33.                 {
  34.                     write(fp_o_2,buffer_o,sizeof(buffer_o));
  35.                     write(STDOUT_FILENO/*OPPURE 1*/,buffer_o,sizeof(buffer_o));
  36.                 }
  37.                 else
  38.                 {
  39.                     write(fp_o_2,buffer_o,1);
  40.                     write(STDOUT_FILENO/*OPPURE 1*/,buffer_o,1);
  41.                 }
  42.                
  43.                 write(fp_o_2," ",1);
  44.                 ///lseek(fp_o,1,SEEK_CUR);
  45.             }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement