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:

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

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

Attachments
#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];	  		//stringa da cercare

char sizeLineInput[512];  				//dimensione massima della riga da leggere
char sizeLineOutput[512]; 				//dimensione massima della riga da scrivere
char *pStart;			  				//puntatore di appoggio per l'inizio riga
char *pEnd;				  				//puntatore di appoggio per la fine riga
char *token;
int k;
						               

int findWord(FILE *SourceFile, char searchingString[20]){
	
	  	if (SourceFile != NULL){
    		//leggi ciascuna riga del file sorgente (fino ad un massimo di 512 caratteri)
    		while (fgets(sizeLineInput, 512, SourceFile)!=NULL){
    			sizeLineOutput[0] = '\0';
    		    //cerca all'interno della riga appena letta, tutte le occorrenze
    		    //della stringa da sostituire. Viene utilizzato un puntatore di appoggio
    		    pStart = sizeLineInput;
    		    token = strtok(sizeLineInput ," ");
    		    //printf("%s\n",token);
    		    do{
		    		token = strtok('\0',", ");
		    		if (token){ 
		    			//printf("%s\n",token); 
		    		//printf("%s\n",searchingString);
		    		//printf("%s\n",token);
		    		    //printf("%d\n", strcmp(searchingString,token));   		
		    		
		    		//if (token==searchingString) printf("%s\n","CIAO");
		    		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;
 }

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:

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

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;
}
} */

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.

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.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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

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

This article has been dead for over six months. Start a new discussion instead.