Esercizio 1, prova pratica 20/02/2014

From Sistemi Operativi
Revision as of 22:11, 3 March 2015 by Davide.boldrin (talk | contribs) (Created page with "==Soluzione di DBoldrin== <source lang="c"> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]){ FILE *fp; char* pathname = arg...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Soluzione di DBoldrin

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, char* argv[]){
   FILE *fp;
   char* pathname = argv[1];
   int contachar=0;
   int contacolonna=0;
   char carattere[2];
   int n=atoi(argv[argc-1]);
   int c;

   fp = fopen(pathname,"r"); // read mode
 
   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }
 
   printf("il numero di elementi nella colonna %d del file %s sono:\n",n,pathname);
 
  while ((c= getc(fp)) != EOF) {
        if ((carattere[1] = c) == '\n')
		contacolonna=0;      
    else
	contacolonna++;
	if(contacolonna==n){
		contachar++;
	}

}	
	printf("%d\n",contachar);
   fclose(fp);
   return 0;
}