943,973 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 51045
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 4th, 2005
0

Search string in a text file

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ricciola is offline Offline
6 posts
since Oct 2005
Nov 4th, 2005
0

Re: Search string in a text file

Post your code plz....
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Nov 4th, 2005
0

Re: Search string in a text file

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Nov 7th, 2005
0

Re: Search string in a text file

Good morning!
I have resolved my problem! I have used the strtok function....
Thank you
Bye ricciola
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ricciola is offline Offline
6 posts
since Oct 2005
Nov 7th, 2005
0

Re: Search string in a text file

Well, in that case it would be nice to post your solution...
Reputation Points: 55
Solved Threads: 6
Junior Poster
Micko is offline Offline
148 posts
since Aug 2005
Nov 8th, 2005
0

Re: Search string in a text file

Quote 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 >>
Attached Files
File Type: h findWord.h (1.8 KB, 438 views)
Last edited by Dave Sinkula; Nov 8th, 2005 at 11:02 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ricciola is offline Offline
6 posts
since Oct 2005
Apr 4th, 2007
-1

Re: Search string in a text file

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
Reputation Points: 9
Solved Threads: 0
Newbie Poster
rajuss is offline Offline
2 posts
since Mar 2007
Apr 4th, 2007
0

Re: Search string in a text file

Click to Expand / Collapse  Quote originally posted by rajuss ...
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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 4th, 2007
0

Re: Search string in a text file

Click to Expand / Collapse  Quote originally posted by rajuss ...
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
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,744 posts
since May 2006
Apr 9th, 2007
0

rajuss

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
Reputation Points: 9
Solved Threads: 0
Newbie Poster
rajuss is offline Offline
2 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Simulating an Adder
Next Thread in C Forum Timeline: not getting non repeating random numbers many times





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC