Advertisement
Guest User

f14.c by cata

a guest
Dec 9th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. /*
  2. * F14. (3 puncte) Scrieti un program care numara aparitiile unui string
  3. * intr-un fisier dat. Stringul si specificatorul fisierului sunt dati ca
  4. * argumente in linia de comanda.
  5. */
  6.  
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. int stringLen(FILE *f)
  12. {
  13. size_t pos = ftell(f); // Current position
  14. fseek(f, 0, SEEK_END); // Go to end
  15. size_t length = ftell(f); // read the position which is the size
  16. fseek(f, pos, SEEK_SET); // restore original position
  17. return length;
  18. }
  19.  
  20. int main(int argc, char **argv)
  21. {
  22. FILE *input;
  23.  
  24. char *string;
  25.  
  26. int tempWord;
  27. int wordLen;
  28. int appearance;
  29. int fs;
  30. int chars;
  31. int stringFileLen;
  32.  
  33. //We check if we do not have 3 arguments
  34. if(argc != 3)
  35. {
  36. fprintf(stderr, "Three arguments required!\nUse: %s word file\n", argv[0]);
  37. return 1;
  38. }
  39.  
  40. //If we have 3 arguments, it's oK
  41.  
  42. tempWord = strlen(argv[1]);
  43. wordLen = strlen(argv[1]);
  44.  
  45. //if word is null, then stop
  46. if(!wordLen)
  47. {
  48. fprintf(stderr, "The second argument is NULL.\n A word with a minimum of length of 1 is required.\n");
  49. return 1;
  50. }
  51.  
  52. //if word is not null, it's ok
  53. input = fopen(argv[2], "r");
  54.  
  55. //We check if the file works
  56. if(input == NULL)
  57. {
  58. perror(argv[2]);
  59. return 1;
  60. }
  61.  
  62. stringFileLen = stringLen(input);
  63. printf("MARIME: %d\n", stringFileLen);
  64. //We allocate memory to string, which is equal to the length of argv[1]
  65. string = (malloc((sizeof(char)*(wordLen + 1))));
  66.  
  67. if(string == NULL)
  68. {
  69. perror("malloc");
  70. fclose(input);
  71. return 1;
  72. }
  73. appearance = 0;
  74. chars = 0;
  75. while(tempWord == wordLen)
  76. {
  77. if((chars + wordLen) <= stringFileLen)
  78. {
  79.  
  80. //We read every wordLen string of input and place it on the tempWord
  81. tempWord = fread(string, sizeof(char), wordLen, input);
  82.  
  83. if(ferror(input))
  84. {
  85. perror(argv[2]);
  86. fclose(input);
  87. return 1;
  88. }
  89.  
  90. string[wordLen] = '\0';
  91. if(strcmp(string, argv[1]) == 0)
  92. appearance++;
  93. fs = fseek(input, 1 - wordLen, SEEK_CUR);
  94. chars++;
  95.  
  96. if(fs == -1)
  97. {
  98. fprintf(stderr, "Searching Error...\n");
  99. fclose(input);
  100. return 1;
  101. }
  102.  
  103. }
  104. else
  105. break;
  106. }
  107. if(appearance < 1) fprintf(stdout, "The %s word does not belong to the file %s.\n", argv[1], argv[2]);
  108. else
  109. {
  110. if(appearance == 1) fprintf(stdout, "The %s word appears only once in the file %s.\n", argv[1], argv[2]);
  111. else fprintf(stdout, "The %s word appears %d times in the file %s.\n", argv[1], appearance, argv[2]);
  112. }
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement