iuliaa

ex1

Oct 14th, 2020
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6.  
  7. int main(int argc, char *argv[]){
  8.     char input[10], output[10], ch;
  9.     int smallch=0, bigch=0, digits=0, apparitions=0, total=0;
  10.     int f1,f2;
  11.     strcpy(input, argv[1]);
  12.     strcpy(output, argv[2]);
  13.     ch=argv[3][0];
  14.     f1=open(argv[1],O_RDONLY);
  15.    
  16.     f2=open(argv[2],O_WRONLY);
  17.     if(f1==-1)
  18.         printf("cannot open input file\n");
  19.     if(f1==-1)
  20.         printf("cannot open output file\n");
  21.     //read file
  22.  
  23.     char car;
  24.     int r;
  25.     while((r=read(f1,&car,1))>0){
  26.         if(car>=97&&car<=122){
  27.             smallch++;
  28.         }
  29.         else if(car>=65&&car<=90){
  30.             bigch++;
  31.         }
  32.         else if(car>=48&&car<=57){
  33.             digits++;
  34.         }
  35.         if(car==ch)
  36.             apparitions++;
  37.         total++;
  38.     }
  39.  
  40.     //printf("%d %d %d %d", smallch,bigch,apparitions,total);
  41.     char string[50],aux[5];
  42.     int w;
  43.     itoa(smallch,aux,10);
  44.     strcpy(string,"numar litere mici: ");
  45.     strcat(string, aux);
  46.     strcat(string, "\n");
  47.     w=write(f2,string,strlen(string));
  48.  
  49.     itoa(bigch,aux,10);
  50.     strcpy(string,"numar litere mari: ");
  51.     strcat(string, aux);
  52.     strcat(string, "\n");
  53.     w=write(f2,string,strlen(string));
  54.  
  55.     itoa(digits,aux,10);
  56.     strcpy(string,"numar cifre: ");
  57.     strcat(string, aux);
  58.     strcat(string, "\n");
  59.     w=write(f2,string,strlen(string));
  60.  
  61.     itoa(apparitions,aux,10);
  62.     strcpy(string,"numar aparitii caracter: ");
  63.     strcat(string, aux);
  64.     strcat(string, "\n");
  65.     w=write(f2,string,strlen(string));
  66.  
  67.     itoa(total,aux,10);
  68.     strcpy(string,"dimensiune fisier: ");
  69.     strcat(string, aux);
  70.     w=write(f2,string,strlen(string));
  71.    
  72.     if(close(f1)<0){
  73.         perror("eroare inchidere fisier citire");
  74.         exit(1);
  75.     }
  76.     if(close(f2)<0){
  77.         perror("eroare inchidere fisier scriere");
  78.         exit(1);
  79.     }
  80.     //f=close(f1);
  81.     //g=close(f2);
  82.    
  83.     //return 0;
  84. }
Add Comment
Please, Sign In to add comment