Search string in a text file

Reply

Join Date: Oct 2005
Posts: 6
Reputation: ricciola is an unknown quantity at this point 
Solved Threads: 0
ricciola ricciola is offline Offline
Newbie Poster

Search string in a text file

 
0
  #1
Nov 4th, 2005
I have a problem. I have a function that search a string in a text file. This function return an int: 0 if the string I must be search isn't in the file, 1 otherwise.
The problem is: if the searching string is "input1" and in the file there is a string "counter_input1" the function return 1 instead must be return 0.
Bye
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Search string in a text file

 
0
  #2
Nov 4th, 2005
Post your code plz....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,331
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1453
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Search string in a text file

 
0
  #3
Nov 4th, 2005
when the string is found, check the character immediately before and after to see if it is a space. If not a space, then the string is not what you want.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: ricciola is an unknown quantity at this point 
Solved Threads: 0
ricciola ricciola is offline Offline
Newbie Poster

Re: Search string in a text file

 
0
  #4
Nov 7th, 2005
Good morning!
I have resolved my problem! I have used the strtok function....
Thank you
Bye ricciola
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: Search string in a text file

 
0
  #5
Nov 7th, 2005
Well, in that case it would be nice to post your solution...
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 6
Reputation: ricciola is an unknown quantity at this point 
Solved Threads: 0
ricciola ricciola is offline Offline
Newbie Poster

Re: Search string in a text file

 
0
  #6
Nov 8th, 2005
Originally Posted by Micko
Well, in that case it would be nice to post your solution...
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <malloc.h>
  6.  
  7. #define IOError -1
  8. #define MEMORYError -2
  9. #define NOError 0
  10.  
  11. const char searchingString[20]; //string to search
  12.  
  13. char sizeLineInput[512];
  14. char sizeLineOutput[512];
  15. char *pStart;
  16. char *pEnd;
  17. char *token;
  18. int k;
  19.  
  20.  
  21. int findWord(FILE *SourceFile, char searchingString[20]){
  22.  
  23. if (SourceFile != NULL){
  24. while (fgets(sizeLineInput, 512, SourceFile)!=NULL){
  25. sizeLineOutput[0] = '\0';
  26. pStart = sizeLineInput;
  27. token = strtok(sizeLineInput ," ");
  28. do{
  29. token = strtok('\0',", ");
  30. if (token){
  31.  
  32. if(strcmp(token,searchingString) == 0){
  33. //printf("%d\n",k);
  34. k = 0;
  35. }
  36. if(strcmp(token,searchingString) == -1){
  37. //printf("%d\n",k);
  38. k = -1;
  39. }
  40. if(strcmp(token,searchingString) == 1){
  41. //printf("%d\n",k);
  42. k = 1;
  43. }
  44. }
  45. }while(token);
  46. //printf("%d\n",k);
  47. if (k==0) return k;
  48. }
  49. }
  50. return -1;
  51. }
<< moderator edit: added [code][/code] tags >>
Last edited by Dave Sinkula; Nov 8th, 2005 at 11:02 am.
Attached Files
File Type: h findWord.h (1.8 KB, 37 views)
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: rajuss is an unknown quantity at this point 
Solved Threads: 0
rajuss rajuss is offline Offline
Newbie Poster

Re: Search string in a text file

 
-1
  #7
Apr 4th, 2007
hi i m trying but couldnt able to do the progrm.
the program is in c & its on finding a string from a text file.
so please help me out to doing it
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,028
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 175
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Search string in a text file

 
0
  #8
Apr 4th, 2007
Originally Posted by rajuss View Post
hi i m trying but couldnt able to do the progrm.
the program is in c & its on finding a string from a text file.
so please help me out to doing it
Sorry, I can not make sense of what is your question or problem.
Could you state it in other words?.

Did you tried the posted code and it didn't work for you? or you are having problems in creating a program that does the same thing?.
Last edited by Aia; Apr 4th, 2007 at 7:17 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Search string in a text file

 
0
  #9
Apr 4th, 2007
Originally Posted by rajuss View Post
hi i m trying but couldnt able to do the progrm.
the program is in c & its on finding a string from a text file.
so please help me out to doing it
Sure. If you would
1) explain what you've tried
2) show what you've tried (read this)
3) not hijack someone else's thread -- make your own
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 2
Reputation: rajuss is an unknown quantity at this point 
Solved Threads: 0
rajuss rajuss is offline Offline
Newbie Poster

rajuss

 
0
  #10
Apr 9th, 2007
hi, i have done the following codding its of searching perticular string
from text file. but it couldnt work please help me out


  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #define NULL 0
  6. //char cut(char,char);
  7. main()
  8. {
  9. clrscr();
  10. int i=0,j=0;
  11. char s[18]={"<nx-charging:date>"};
  12. char s1[23]={"<nx-charging:serviceID>"};
  13. char s2[25]={"<nx-charging:finalStatus>"};
  14. //char s[]={"<nx-charging:date>"};
  15. FILE *fpt;
  16. char ch,c,b[23],a[18];
  17. if((fpt=fopen("d:\\sample2.txt","r"))==NULL)
  18. printf("\nERROR - cannot open the file ");
  19. else
  20. do
  21. {
  22. c=fgetc(fpt);
  23. a[i]=c;
  24. i++;
  25. a[i];
  26. if(i==18)
  27. {
  28. if(strcmp(a,s)==0)
  29. {
  30. /*int k;
  31.   for(k=0;k<18;k++){
  32.   while(a[k]!='<')
  33.   {
  34.   printf("%s",ch);
  35.   }*/printf("\n%s",a);
  36. break;
  37. }
  38. /* else
  39.   cut(a,n);
  40.  /*{
  41.   int l;
  42.   for(l=0;l<strlen(a);l++)
  43.   {
  44.   if(l!=strlen(a)-1)
  45.   {
  46.   a[i]=a[i+1];
  47.   }
  48.   else
  49.   {
  50.   goto m;
  51.   }
  52.   }
  53.   }*/
  54. }
  55. c=fgetc(fpt);
  56. b[j++]=c;
  57. if(j==23)
  58. {
  59. if(strcmp(b,s1)==0)
  60. {
  61. /*int j;
  62.   for(j=0;j<18;j++){
  63.   /*while(a[j]!='<')
  64.   {
  65.   printf("%c",ch);
  66.   }*/printf("\n%s",b);break;
  67. }
  68. }
  69. ch=fgetc(fpt);
  70. b[j++]=ch;
  71. if(j==25)
  72. {
  73. if(strcmp(b,s2)==0)
  74. {
  75. /*int j;
  76.   for(j=0;j<18;j++){
  77.   /*while(a[j]!='<')
  78.   {
  79.   printf("%c",ch);
  80.   }*/printf("\n%s",b);break;
  81. }
  82. }
  83. }while(c!=EOF);
  84. fclose(fpt);
  85. getch();
  86. return 0;
  87. }
  88. /*char cut(char v[18],char m)
  89. {
  90. int l;
  91. for(l=0;l<strlen(v);l++)
  92. {
  93. if(l!=strlen(v)-1)
  94. {
  95. v[l]=v[l+1];
  96. }
  97. else
  98. v[l++]=m;
  99. }
  100. } */
Last edited by Ancient Dragon; Apr 9th, 2007 at 12:04 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC