Thread: Reading from a file and comparing the text to a string

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Reading from a file and comparing the text to a string

    Hi,
    I am currently writing a program to scan through some files, find a string (or preferably keywords), Print the line to a file and count how many times it occurs. However each time I test the input string using an if statement, I get a positive result as long as the file contains some sort of text.

    I am new to c# so it is probably a misunderstanding of the classes I'm using but any help will be appreciated.

    Thank you
    Note: I am not at the stage of writing the results to a file yet, I am writing to the console for testing purposes.

    Code:
     using (StreamReader read = File.OpenText(file))
                               {
                                    string line;
                                    while ((line = read.ReadLine()) != null)
                                    {
                                        if (line.Contains("Test String")) ;
                                        {
                                            count++;
                                            
                                            Console.WriteLine(count);
                                            Console.WriteLine(line);
                                        }
                                        
                                    }
                                    read.Close();
                                }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    I'm sorry just noticed a ';' after the if statement, it's not working now, happy days...
    Last edited by souslatable; 06-09-2011 at 08:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a text file into a string - fread?
    By frankchester in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 12:49 PM
  2. reading a string from a text file into a 2d array
    By duelord in forum C++ Programming
    Replies: 2
    Last Post: 11-22-2009, 07:29 AM
  3. Reading text file into a string obj
    By thetinman in forum C++ Programming
    Replies: 8
    Last Post: 09-02-2008, 11:42 AM
  4. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM
  5. reading string and integers from a text file
    By Gator in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 02:17 PM