11 Replies - 2854 Views - Last Post: 03 May 2011 - 01:10 PM

#1 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Reading/comparing contents of text file

Posted 30 April 2011 - 02:13 PM

I am having a little trouble with reading the contents of a text file for a Windows Phone Application. The text file is an existing file with one word per line. I am trying to check a variable against that text file, if the variable is contained in the text file it would return true if not return false (obviously).

Here is the code I have tried, it returns an error ("Attempt to access the method failed: System.IO.StreamReader..ctor(System.String)")

public bool CheckIfWord(string wordToBeTested)
        {
            string testing = wordToBeTested.ToLower();

            string line;
            StreamReader FileRead = new StreamReader("/dictionary.txt");
            
                while ((line = FileRead.ReadLine()) != null)
                {

                    if (testing == line)
                        verification = true;
                    else
                        verification = false;                    
                }               
            

            return verification;
        }



Please let me know what I am doing wrong...

This post has been edited by d.buckner: 30 April 2011 - 03:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading/comparing contents of text file

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Reading/comparing contents of text file

Posted 30 April 2011 - 02:25 PM

Are you building a Windows Phone application or is it for a Windows Mobile device?
Was This Post Helpful? 0
  • +
  • -

#3 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: Reading/comparing contents of text file

Posted 30 April 2011 - 03:02 PM

View PostCore, on 30 April 2011 - 03:25 PM, said:

Are you building a Windows Phone application or is it for a Windows Mobile device?

This is for a Windows Phone application, sorry should have said that in the original post.
Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: Reading/comparing contents of text file

Posted 30 April 2011 - 05:30 PM

There are good news and bad news in your situation. The bad news is the fact that you cannot use the constructor with the path passed as a string due to platform restrictions. According to MSDN:

Quote

This member has a SecurityCriticalAttribute attribute on Silverlight for Windows Phone, because the attribute was present in Silverlight 3. This attribute restricts this member to internal use. Application code that uses this member throws a MethodAccessException.


The good news: you can compile the file as a Resource and invoke it as such:

StreamResourceInfo resInfo = App.GetResourceStream(new Uri("NAME_OF_ASSEMBLY;component/dictionary.txt", UriKind.Relative));
using (TextReader reader = new StreamReader(resInfo.Stream))
{
     // Other text-related actions
     Debug.WriteLine(reader.ReadToEnd());
}

Was This Post Helpful? 0
  • +
  • -

#5 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 09:32 AM

Ok your example above is now reading the txt file (at least it appears to be), but I am unable to compare the contents to a known string. Obviously I am doing something else wrong. I think the problem may be in my while loop. Again, all I want to do is loop through the lines in the text file, and compare them to another string. If the text file contains the string, verification should return true. Otherwise return false. Right now it is always returning false. My current code is below:

 public bool CheckIfWord(string wordToBeTested)
        {
            string testing = wordToBeTested.ToLower();
            string line;
            //StreamReader FileRead = new StreamReader("/dictionary.txt");
            StreamResourceInfo resInfo = App.GetResourceStream(new Uri("/DictionaryTest;component/dictionary.txt", UriKind.Relative));
            using (TextReader reader = new StreamReader(resInfo.Stream))
            {

                while ((line = reader.ReadLine()) != null)
                {

                    if (line != testing)
                    {
                        verification = false;
                        return verification;                        
                    }
                    verification = true;
                }

                Debug.WriteLine(reader.ReadToEnd());
            }

            return verification;
            
        }


This post has been edited by d.buckner: 03 May 2011 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 10:40 AM

Have you tried setting a breakpoint at the while loop and walk through the code? Your logic is not correct, assuming that there is more than 1 line in the text file.
Was This Post Helpful? 0
  • +
  • -

#7 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 10:47 AM

View Posteclipsed4utoo, on 03 May 2011 - 11:40 AM, said:

Have you tried setting a breakpoint at the while loop and walk through the code? Your logic is not correct, assuming that there is more than 1 line in the text file.


I have set the breakpoint, the 'line' variable (which is supposed to be the next line in the text file, and there are several lines in the text file) is null. Apparently, my logic is wrong, what did I miss in the logic?
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 10:52 AM

Is it null the first time through?

As for your logic, if the file has more than 1 line, the method will ALWAYS return false unless the first line matches. If the first line fails, the method will return false. It doesn't check the other lines.
Was This Post Helpful? 0
  • +
  • -

#9 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 11:11 AM

View Posteclipsed4utoo, on 03 May 2011 - 11:52 AM, said:

Is it null the first time through?

As for your logic, if the file has more than 1 line, the method will ALWAYS return false unless the first line matches. If the first line fails, the method will return false. It doesn't check the other lines.



Yes, it is null the first time through. Apparently it is still not reading the file. Since my while loop is obviously not correct how do I get it to loop through all the lines then?

This post has been edited by d.buckner: 03 May 2011 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 11:44 AM

here is how I would write the logic for finding the string in the file...

var resInfo = Application.GetResourceStream(new Uri("dictionary.txt", UriKind.Relative);
using (TextReader reader = new StreamReader(resInfo.Stream))
{
        bool verification = false;
	string line = string.Empty;
	
	while (reader.Peek() >= 0)
	{
		line = reader.ReadLine();

		// assuming you don't care about uppercase/lowercase
		if (string.Equals(line, testing, StringComparison.InvariantCultureIgnoreCase)
		{
		   verification = true;                        
		}
	}
}

return verification;



Basically, you only want to set verification to true if there's an actual match. Any other time, it will be false.
Was This Post Helpful? 1
  • +
  • -

#11 d.buckner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 04-December 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 12:22 PM

Ok, thanks for your help. With some minor adjustments, and your assistance, I was able to get it to do what I want. I had to create the verification variable outside the method and make it public. Here is my version:

public bool verification;
       
        public bool CheckIfWord(string wordToBeTested)
        {
            string testing = wordToBeTested.ToLower();
            
            //StreamReader FileRead = new StreamReader("/dictionary.txt");
            var resInfo = Application.GetResourceStream(new Uri("/DictionaryTest;component/dictionary.txt", UriKind.Relative));
            using (TextReader reader = new StreamReader(resInfo.Stream))
            {
        
	            string line = string.Empty;
                verification = false;
	
	            while (reader.Peek() >= 0)
	            {
		            line = reader.ReadLine();

                    if (string.Equals(line, testing, StringComparison.InvariantCulture))
                    {
                        verification = true;
                    }
                    
                    
	            }
             }

        return verification;
            
        }



Thanks again for all your help!!
Was This Post Helpful? 0
  • +
  • -

#12 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Reading/comparing contents of text file

Posted 03 May 2011 - 01:10 PM

Not exactly sure why you would need to do that.

EDITED: sorry, my code had a small bug(since I wrote it freehand). You could have simply moved the bool verification = false; code to where you declare the testing variable. It would still be inside the method(where it should be).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1