954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Search string in a text file

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
:idea: :idea: :idea:

ricciola
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

Post your code plz....

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Good morning!
I have resolved my problem! I have used the strtok function....
Thank you
Bye ricciola

ricciola
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

Well, in that case it would be nice to post your solution...

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 
Well, in that case it would be nice to post your solution...
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>

#define IOError     	-1
#define MEMORYError 	-2
#define NOError      	 0

const char searchingString[20];	  		//string to search

char sizeLineInput[512];  				
char sizeLineOutput[512]; 				
char *pStart;			  				
char *pEnd;				  				
char *token;
int k;
						               

int findWord(FILE *SourceFile, char searchingString[20]){
	
	  	if (SourceFile != NULL){
    			while (fgets(sizeLineInput, 512, SourceFile)!=NULL){
    			sizeLineOutput[0] = '\0';
    		        pStart = sizeLineInput;
    		        token = strtok(sizeLineInput ," ");
    		        do{
		    		token = strtok('\0',", ");
		    		if (token){ 
		    		    		
		       		if(strcmp(token,searchingString) == 0){ 
		    			//printf("%d\n",k);
		    			k = 0;    	
		    		}
		    		if(strcmp(token,searchingString) == -1){
		    			//printf("%d\n",k);
		    			k = -1;
		    		}
		    		if(strcmp(token,searchingString) == 1){
		    			//printf("%d\n",k);
		    			k = 1;
		    		}
		    		}
		    	}while(token);
		    	//printf("%d\n",k);
		    	if (k==0) return k;
		}
    	}
    	return -1;
 }


<< moderator edit: added [code][/code] tags >>

Attachments findWord.h (1.79KB)
ricciola
Newbie Poster
6 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 

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:sad:

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

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?.

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 
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:sad:


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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

hi, i have done the following codding its of searching perticular string
from text file. but it couldnt work please help me out


#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>
#define NULL 0
//char cut(char,char);
main()
{
  clrscr();
  int i=0,j=0;
  char s[18]={"<nx-charging:date>"};
  char s1[23]={"<nx-charging:serviceID>"};
  char s2[25]={"<nx-charging:finalStatus>"};
  //char s[]={"<nx-charging:date>"};
  FILE *fpt;
  char ch,c,b[23],a[18];
  if((fpt=fopen("d:\\sample2.txt","r"))==NULL)
    printf("\nERROR - cannot open the file ");
  else
    do
    {
      c=fgetc(fpt);
      a[i]=c;
      i++;
      a[i];
      if(i==18)
      {
 if(strcmp(a,s)==0)
 {
   /*int k;
   for(k=0;k<18;k++){
   while(a[k]!='<')
   {
     printf("%s",ch);
   }*/printf("\n%s",a);
   break;
 }
       /* else
   cut(a,n);
 /*{
   int l;
   for(l=0;l<strlen(a);l++)
   {
     if(l!=strlen(a)-1)
     {
       a[i]=a[i+1];
     }
     else
     {
       goto m;
     }
   }
  }*/
       }
      c=fgetc(fpt);
      b[j++]=c;
      if(j==23)
      {
 if(strcmp(b,s1)==0)
 {
   /*int j;
   for(j=0;j<18;j++){
   /*while(a[j]!='<')
   {
     printf("%c",ch);
   }*/printf("\n%s",b);break;
 }
      }
      ch=fgetc(fpt);
      b[j++]=ch;
      if(j==25)
      {
 if(strcmp(b,s2)==0)
 {
   /*int j;
   for(j=0;j<18;j++){
   /*while(a[j]!='<')
   {
     printf("%c",ch);
   }*/printf("\n%s",b);break;
 }
      }
    }while(c!=EOF);
  fclose(fpt);
  getch();
  return 0;
}
/*char cut(char v[18],char m)
{
int l;
for(l=0;l<strlen(v);l++)
{
if(l!=strlen(v)-1)
{
v[l]=v[l+1];
}
else
v[l++]=m;
}
} */
rajuss
Newbie Poster
2 posts since Mar 2007
Reputation Points: 9
Solved Threads: 0
 

>#define NULL 0

I get a problem with that?

>clrscr();

Why, no need for this or conio.h which is not standard.

Read the following...Looks like you're comparing chars instead of c-style strings.

http://www.daniweb.com/tutorials/tutorial45806.html

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

use fgets() instead of fgetc() and your program will work better and be a lot less complicated.

delete line 5, NULL is defined in standard c header files.

remove the braces on lines 11, 12 and 13. They are not necessary there.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

serch string in text file

akshay1
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

Welcome to the forum, Akshay1.

This thread is 3 years old - how about starting a new one? More specifics of your assignment, would help. Also, what is stopping you from including string.h and using strstr() to find your string in the text file.?

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
#include"string.h"
#include"process.h"

void main()
{
FILE *fp;
char ch, search_string[25], temp[25];
int i=0, k, search_len;
long int count=0;

puts("Enter a string to be searched\n");
scanf("%s",search_string);
search_len=strlen(search_string);
printf("%d",search_len);

if((fp=fopen("site_startup.tra","r"))==NULL)
{
puts("open error");
getch();
exit(0);
}

while((ch=fgetc(fp))!=EOF)
{
if(i

shellk007
Newbie Poster
1 post since Feb 2011
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You