jueves, 15 de diciembre de 2011

PRACTICA LABORATORIO (CARCTER)

#include <unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#define MAXLON 1000
void *cuenta (void *dato) {
int pos, cont= 0, leidos;
char *nombre= (char *) dato, cadena[MAXLON];
int fd;

fd = open (nombre, O_RDONLY);
while ((leidos= read (fd, cadena, MAXLON))!= 0)
for (pos= 0; pos< leidos; pos++)
if ((cadena[pos]== 'a') || (cadena[pos]== 'A'))
cont++;
printf ("Fichero %s: %d caracteres 'a' o 'A' encontrados\n", nombre, cont);
close (fd);
pthread_exit (NULL);
}
int main (int argc, char *argv[]) {
pthread_t hilo;
if (argc!= 2) {
printf ("Indica el nombre de un fichero.\n");
exit(0);
}
pthread_create (&hilo, NULL, cuenta, (void *) argv[1]);
pthread_join (hilo, NULL);
return 0;
}