searching in a text file

what is wrong with my code?

Page 1 of 1

6 Replies - 586 Views - Last Post: 04 September 2009 - 05:18 AM Rate Topic: -----

#1 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

searching in a text file

Posted 04 September 2009 - 03:53 AM

hi evry body
I wrote a function for searching in a text file and if favorit string exist, report me, but it don't work properly,
private void searchinfile(string fileadd,string text)
		{
			StreamReader s = new StreamReader(fileadd);
			string currentLine;
			string searchString = text;
			bool foundText = false;

			do
			{
				currentLine = s.ReadLine();
				if (currentLine != null)
				{
					foundText = currentLine.Contains(searchString);
				}
			}
			while (currentLine != null && !foundText);

			if (foundText)
			{
				MessageBox.Show("the word is repeated, do you want to save it?");
			}

		}


what is wrong?
best regards

This post has been edited by hamidkhl: 04 September 2009 - 04:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: searching in a text file

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: searching in a text file

Posted 04 September 2009 - 04:18 AM

You tell us what's wrong. Errors? Can't find the string?

I tested that and it seemed to work fine. It found a string I wanted in a textfile I passed to it.
Was This Post Helpful? 0
  • +
  • -

#3 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: searching in a text file

Posted 04 September 2009 - 04:27 AM

View Postjanne_panne, on 4 Sep, 2009 - 03:18 AM, said:

You tell us what's wrong. Errors? Can't find the string?

I tested that and it seemed to work fine. It found a string I wanted in a textfile I passed to it.

all the time MessageBox is shown ,
despite string that I intered is not in file
Was This Post Helpful? 0
  • +
  • -

#4 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: searching in a text file

Posted 04 September 2009 - 04:35 AM

Could you give me the content of the textfile and the string you are searching so I can test with the same data.

Of course I understand if you can't give the data.

I tested with this data:
hello
world
hello
world


and searched for "hello" (Match found" and "a" (Match not found).
Was This Post Helpful? 0
  • +
  • -

#5 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: searching in a text file

Posted 04 September 2009 - 04:48 AM

my file content:
accumulate , at 9/5/2009 4:11:09 PM
Auction , at 9/5/2009 4:12:59 PM


when I insert for example "apple" messagebox shown
I am writing a application that makes flash cards for english lerners and I want if a word is repeated warns to user

This post has been edited by hamidkhl: 04 September 2009 - 04:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: searching in a text file

Posted 04 September 2009 - 05:03 AM

Strange.

Can you post the code of how you call the method?
Could it be that before you check if the word is repeated you accidentally save the word user wrote in the text file? So that's the reason it would print out the textbox all the time. So make sure all the methods which modify the textfile are executed after that searchinfile method.

I tried it like this and works fine:

	public partial class Form1 : Form
	{

		public Form1()
		{
			searchinfile("C:\\temp\\my.txt", "apple");
		}

		private void searchinfile(string fileadd, string text)
		{
			StreamReader s = new StreamReader(fileadd);
			string currentLine;
			string searchString = text;
			bool foundText = false;

			do
			{
				currentLine = s.ReadLine();
				if (currentLine != null)
				{
					foundText = currentLine.Contains(searchString);
				}
			}
			while (currentLine != null && !foundText);

			if (foundText)
			{
				MessageBox.Show("the word is repeated, do you want to save it?");
			}

		}

	}


Was This Post Helpful? 1
  • +
  • -

#7 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: searching in a text file

Posted 04 September 2009 - 05:18 AM

YES, thats it
thank u very much
how stupid I am!!!!!!!!
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1