Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <pthread.h>
  7. #include <regex.h>
  8.  
  9. int match(const char *string, const char *pattern)
  10. {
  11. regex_t re;
  12. if (regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) return 0;
  13. int status = regexec(&re, string, 0, NULL, 0);
  14. regfree(&re);
  15. if (status != 0) return 0;
  16. return 1;
  17. }
  18.  
  19. int wykonaj1(char *filename)
  20. {
  21. // count the number of lines in the file called filename
  22. FILE *fp = fopen(filename,"r");
  23. int ch=0;
  24. int lines=0;
  25.  
  26. if (fp == NULL){
  27. printf("CO DO KURWY");
  28. return 0;
  29. }
  30.  
  31. while ((ch = fgetc(fp)) != EOF)
  32. {
  33. if (ch == '\n')
  34. lines++;
  35. }
  36. fclose(fp);
  37. printf("%d\n", lines);
  38. }
  39.  
  40.  
  41.  
  42. int wykonaj2(char *filename){
  43.  
  44.  
  45. FILE *fp = fopen(filename,"r");
  46. int ch=0;
  47. int words=0;
  48.  
  49. if (fp == NULL){
  50. printf("CO DO KURWY");
  51. return 0;
  52. }
  53. char cur_str[10000];
  54. int counter = 0;
  55. while ((ch = fgetc(fp)) != EOF)
  56. {
  57. if (ch == '\n'){
  58. counter = 0;
  59. words += match(cur_str, "pipe");
  60. }
  61. cur_str[counter] = ch;
  62. counter++;
  63.  
  64. }
  65.  
  66.  
  67.  
  68. printf("%d\n", words);
  69.  
  70. }
  71. void error(char *msg)
  72. {
  73. printf("%s", msg);
  74. }
  75.  
  76. int main(){
  77. pthread_t t0;
  78. pthread_t t1;
  79.  
  80. if(pthread_create(&t0,NULL,wykonaj1,"dictionary.txt")==-1)
  81. error("Nie mozna utworzyc watku t0");
  82. if(pthread_create(&t1,NULL,wykonaj2,"dictionary.txt")==-1)
  83. error("Nie mozna utworzyc watku t1");
  84.  
  85. void* result;
  86. if(pthread_join(t0,&result)==-1)
  87. error("Blad oczekiwania na zakonczenie watku t0");
  88. if(pthread_join(t1,&result)==-1)
  89. error("Blad oczekiwania na zakonczenie watku t0");
  90.  
  91. //printf("licznk na koniec ma wartosc = %i ", licznik);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement