Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.85 KB | None | 0 0
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. #define MAX 1000
  7. #define MIN 128
  8. #define BLOC 512
  9. #define LINE 256
  10. #define SBLOC 128
  11. #define TRUE 1
  12. #define FALSE 0
  13.  
  14. int main()
  15. {
  16.  
  17. FILE *fp;
  18. char *buffer = malloc(sizeof(char)*(MAX*MAX));
  19. //char *buffer1 = malloc(sizeof(char)*MIN);
  20. //char buffer1[MAX];
  21. char*startLoc =malloc(sizeof(char)*(MAX));
  22. char*currLoc = malloc(sizeof(char)*(MAX));
  23. char c = 0;
  24.  
  25. int line = 0;
  26. int blockCount = 0;
  27. int scrapsCount = 0;
  28. long bookmark = 0;
  29.  
  30. if(buffer == NULL)
  31. {
  32.   perror("Error allocating memory.");
  33.   return(-1);
  34. }
  35.  
  36. /*
  37. if(buffer1 == NULL)
  38. {
  39.   perror("Error allocating memory.");
  40.   return(-1);
  41. }*/
  42.  
  43.  
  44. for(int i = 0; i < MAX; i++)
  45. {
  46.   buffer[i] = '\0';
  47.   //buffer1[i] = '\0';
  48. }
  49.  
  50. char* filename = malloc(sizeof(char)*20);
  51. char* word = malloc(sizeof(char)*20);
  52.  
  53. if(filename == NULL)
  54. {
  55.   perror("Error allocating memory");
  56.   return(-1);
  57. }
  58.  
  59. printf("Please enter the name of the file you wish to open.");
  60. scanf("%s", filename);
  61.  
  62. printf("Please enter the word you wish to seek in this file.");
  63. scanf("%s", word);
  64.  
  65. if((fp = fopen(filename, "rb")) == NULL)
  66. {
  67.   perror("Error opening file.");
  68.   return(-1);
  69. }
  70.  
  71. //Write the content of the file to output.
  72. while(!feof(fp))
  73. {
  74.     fread(buffer, sizeof(char*), BLOC, fp);
  75.     printf("\n\nCluster %d\n", blockCount);
  76.     printf("\\nn%s\n", buffer);
  77.  
  78.     for(startLoc = buffer; *startLoc != '\0'; *(startLoc++))
  79.       {
  80.           if(*startLoc == "\n")
  81.             line++;
  82.  
  83.           if((currLoc = strstr(startLoc, word))!= NULL)
  84.           {
  85.             startLoc = currLoc;
  86.             printf("\n\nThe word %s is found on line %d", word, line);
  87.           }
  88.  
  89.       }
  90. }
  91. printf("\n\nThis is a redundant test mechanic");
  92.  
  93. fclose(fp);
  94. free(filename);
  95. free(buffer);
  96. free(word);
  97. //free(buffer1);
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement