Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <pwd.h>
- #include <grp.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- char buffer[BUFSIZ];
- void encuentra(char *word, char *archivo) {
- struct stat buf;
- int i;
- int fd_current;
- char *palabra = word;
- char current_line[1000];
- int nbytes;
- int index_current_line;
- int index = 0;
- if((fd_current = open(archivo, O_RDONLY)) < 0) {
- fprintf(stdout, "ERROR: Archivo %s no encontrado\n", archivo);
- }else{
- fprintf(stdout, "<- DESPLIEGA LA INFORMACION DEL ARCHIVO %s ->\n", archivo);
- while((nbytes = read(fd_current, buffer, sizeof(buffer))) > 0){
- for(i = 0;i < nbytes;i++){
- memset((void*)¤t_line, 0, sizeof(char)*1000);
- index_current_line = 0;
- while(buffer[i] != '\n'){
- current_line[index_current_line]=buffer[i];
- index_current_line++;
- i++;
- }
- //fprintf(stdout,"Linea %d: %s\n",index, current_line);
- //index++;
- char *string;
- if(string = strstr(current_line, palabra)){
- fprintf(stdout,"%s\n",current_line);
- }
- }
- }
- }
- }
- int main(int argc, char *argv[]) {
- int i;
- if (argc < 3) {
- fprintf(stderr, "ERROR. Forma de uso: encuentra palabra archivo1 [archivo2 ... archivon]\n\n");
- return -1;
- }
- for (i = 2; i < argc; i++) {
- encuentra(argv[1],argv[i]);
- }
- return EXIT_SUCCESS;
- }
- talves este de pueda ayudar para el ejercicio dos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement