Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <locale.h>
  5. #include <conio.h>
  6.  
  7.  
  8. /**************************
  9. Användning av strstr(), tar
  10. in en sträng och kollar efter
  11. en mindre sträng i strängen!!
  12. ***************************/
  13. /* const char haystack[20] = "TutorialsPoint";
  14. const char needle[10] = "Point";
  15. char *ret;
  16.  
  17. ret = strstr(haystack, needle);
  18.  
  19. printf("The substring is: %s\n", ret);*/
  20. /***************
  21. Kvar att fixa:
  22. -avsluta med #q,
  23. både vid filnam
  24. och sök ord!
  25.  
  26. -Error, word not
  27. found måste oxå
  28. funka!
  29. ***************/
  30. int main()
  31. {
  32. setlocale (LC_ALL, "");
  33.  
  34. FILE *f_ptr;
  35. char temp[100];
  36. char text_file[30];
  37. char restart;
  38. int file_check = 0;
  39.  
  40. do
  41. {
  42. do
  43. {
  44. printf("Textfile to open: ");
  45. gets(text_file);
  46. if(strcmp(text_file,"#q")==0) //hmmmm
  47. {
  48. printf("Oh okey... bye then!");
  49. exit(1);
  50. }
  51. else if((f_ptr = fopen(text_file, "r"))==0)
  52. {
  53. perror(text_file);
  54. file_check=0;
  55. }
  56. else
  57. file_check=1;
  58. }while(file_check==0);
  59. // fclose(f_ptr);
  60.  
  61.  
  62. if(f_ptr!=NULL)
  63. {
  64. char line[200];
  65. int i=0;
  66. int text_line=0,textline=0;
  67.  
  68. printf("Plese enter a word so search for: ");
  69. scanf("%s",temp);
  70. if(strcmp(temp,"#q")==0)
  71. {
  72. printf("Oh okey... bye then!");
  73. exit(1);
  74. }
  75. textline=NULL;
  76. while(fgets(line, sizeof(line),f_ptr)!=NULL)
  77. {
  78. text_line++;
  79. if(strstr(line,temp)!=NULL)
  80. {
  81. while(i==0)
  82. {
  83. printf("'%s' found on lines: ", temp);
  84. i++;
  85. }
  86. textline=text_line;
  87. printf(" %d,", textline);
  88. }
  89. // printf("%d",text_line);
  90. }
  91. if(textline==NULL) //räknar inte med ord som bara finns på en rad:(
  92. printf("Error!! Word not found...");
  93. }
  94. else
  95. fclose(f_ptr);
  96.  
  97. printf("\n\nPlease press 'space' to restart the program!");
  98. fflush(stdin);
  99. restart=_getch();
  100. system("cls");
  101. }
  102. while(restart==' ');
  103.  
  104.  
  105. return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement