Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <locale.h>
- #include <conio.h>
- /**************************
- Användning av strstr(), tar
- in en sträng och kollar efter
- en mindre sträng i strängen!!
- ***************************/
- /* const char haystack[20] = "TutorialsPoint";
- const char needle[10] = "Point";
- char *ret;
- ret = strstr(haystack, needle);
- printf("The substring is: %s\n", ret);*/
- /***************
- Kvar att fixa:
- -avsluta med #q,
- både vid filnam
- och sök ord!
- -Error, word not
- found måste oxå
- funka!
- ***************/
- int main()
- {
- setlocale (LC_ALL, "");
- FILE *f_ptr;
- char temp[100];
- char text_file[30];
- char restart;
- int file_check = 0;
- do
- {
- do
- {
- printf("Textfile to open: ");
- gets(text_file);
- if(strcmp(text_file,"#q")==0) //hmmmm
- {
- printf("Oh okey... bye then!");
- exit(1);
- }
- else if((f_ptr = fopen(text_file, "r"))==0)
- {
- perror(text_file);
- file_check=0;
- }
- else
- file_check=1;
- }while(file_check==0);
- // fclose(f_ptr);
- if(f_ptr!=NULL)
- {
- char line[200];
- int i=0;
- int text_line=0,textline=0;
- printf("Plese enter a word so search for: ");
- scanf("%s",temp);
- if(strcmp(temp,"#q")==0)
- {
- printf("Oh okey... bye then!");
- exit(1);
- }
- textline=NULL;
- while(fgets(line, sizeof(line),f_ptr)!=NULL)
- {
- text_line++;
- if(strstr(line,temp)!=NULL)
- {
- while(i==0)
- {
- printf("'%s' found on lines: ", temp);
- i++;
- }
- textline=text_line;
- printf(" %d,", textline);
- }
- // printf("%d",text_line);
- }
- if(textline==NULL) //räknar inte med ord som bara finns på en rad:(
- printf("Error!! Word not found...");
- }
- else
- fclose(f_ptr);
- printf("\n\nPlease press 'space' to restart the program!");
- fflush(stdin);
- restart=_getch();
- system("cls");
- }
- while(restart==' ');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement