Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <pwd.h>
  6. #include <grp.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. char buffer[BUFSIZ];
  12.  
  13. void encuentra(char *word, char *archivo) {
  14.  
  15. struct stat buf;
  16. int i;
  17. int fd_current;
  18. char *palabra = word;
  19. char current_line[1000];
  20. int nbytes;
  21. int index_current_line;
  22. int index = 0;
  23.  
  24. if((fd_current = open(archivo, O_RDONLY)) < 0) {
  25. fprintf(stdout, "ERROR: Archivo %s no encontrado\n", archivo);
  26. }else{
  27. fprintf(stdout, "<- DESPLIEGA LA INFORMACION DEL ARCHIVO %s ->\n", archivo);
  28. while((nbytes = read(fd_current, buffer, sizeof(buffer))) > 0){
  29. for(i = 0;i < nbytes;i++){
  30. memset((void*)&current_line, 0, sizeof(char)*1000);
  31. index_current_line = 0;
  32. while(buffer[i] != '\n'){
  33. current_line[index_current_line]=buffer[i];
  34. index_current_line++;
  35. i++;
  36. }
  37. //fprintf(stdout,"Linea %d: %s\n",index, current_line);
  38. //index++;
  39. char *string;
  40. if(string = strstr(current_line, palabra)){
  41. fprintf(stdout,"%s\n",current_line);
  42. }
  43.  
  44. }
  45. }
  46. }
  47. }
  48.  
  49. int main(int argc, char *argv[]) {
  50.  
  51. int i;
  52. if (argc < 3) {
  53. fprintf(stderr, "ERROR. Forma de uso: encuentra palabra archivo1 [archivo2 ... archivon]\n\n");
  54. return -1;
  55. }
  56. for (i = 2; i < argc; i++) {
  57. encuentra(argv[1],argv[i]);
  58. }
  59. return EXIT_SUCCESS;
  60. }
  61.  
  62. talves este de pueda ayudar para el ejercicio dos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement