4 Replies - 466 Views - Last Post: 15 May 2013 - 06:57 PM Rate Topic: -----

#1 iostreamstdio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-May 13

searching simple word in a file in c

Posted 15 May 2013 - 07:52 AM

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp;
char str[50],ch,temp[50];
int i;
printf("enter the word to be searched");
gets(str);
fp=fopen("file.txt","r");
if(fp==NULL)
{
	printf("cant open file");
	getch();
	exit();
}
else
{
	while((ch=fgetc(fp))!=EOF)
	{
	i=0;
	while(ch!=' ')
	{
	temp[i]=ch;
	i++;
	ch=fgetc(fp);
	}

	temp[i]='\0';
	if((strcmp(str,temp))==0)
	{
	printf("found");
	}

	}
}

fclose(fp);
getch();
}


After running this code it is not able to come out of the ouput screen after searching the required word. And I am not able to find anything wrong in it. Please help.

Is This A Good Question/Topic? 0
  • +

Replies To: searching simple word in a file in c

#2 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: searching simple word in a file in c

Posted 15 May 2013 - 08:31 AM

Well first of all, you never ever clear temp.

Secondly, Can you kind of explain what you mean by 'doesn't leave the output screen?' Do you mean it doesn't close? Or that your find loop runs forever? Or what?

This post has been edited by ccubed: 15 May 2013 - 08:32 AM

Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1325
  • View blog
  • Posts: 4,551
  • Joined: 19-February 09

Re: searching simple word in a file in c

Posted 15 May 2013 - 03:52 PM

Possibly the words could be at the end of a line or the end of the file, so you may have to check for a newline and EOF here as well.

23	    while(ch!=' ')




isspace
Was This Post Helpful? 1
  • +
  • -

#4 iostreamstdio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-May 13

Re: searching simple word in a file in c

Posted 15 May 2013 - 06:51 PM

View Postccubed, on 15 May 2013 - 08:31 AM, said:

Well first of all, you never ever clear temp.

Secondly, Can you kind of explain what you mean by 'doesn't leave the output screen?' Do you mean it doesn't close? Or that your find loop runs forever? Or what?

I mean it doesn't close. It gets stuck after displaying "found". It is not responding to any key of the keyboard.
Was This Post Helpful? 0
  • +
  • -

#5 iostreamstdio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-May 13

Re: searching simple word in a file in c

Posted 15 May 2013 - 06:57 PM

View Post#define, on 15 May 2013 - 03:52 PM, said:

Possibly the words could be at the end of a line or the end of the file, so you may have to check for a newline and EOF here as well.

23	    while(ch!=' ')




isspace

Thanks a lot. It is now working. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1