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:

Post your code plz....

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.

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

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

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 [co[u][/u]de][/co[u][/u]de] tags >>

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:

commented: Me too Me too - I want to bump a dead thread :( :( :* -1

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

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

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;
}
} */
Member Avatar for iamthwee

>#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

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.

serch string in text file

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

#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<search_len)
        {
            temp[i]=ch;
            ++i;
        }

        if(i==search_len)
        {

            count++;
            temp[i]='\0';
            k=stricmp(search_string,temp);

            if(k==0)
            {
                printf("search successful\n");

            }


            i=0;
            fseek(fp,count,SEEK_SET);

        }
    }
}

this is the easiest way to do it.

commented: To do what, exactly? Show people a bad way to program? If so, then yes, this is one way to write bad (and dangerous) code. -3

I think TextFinding.com may help you. It is a useful and easy tool to search string in a text file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.