1

Please have a look at the below code.

DirectoryInfo directory = new DirectoryInfo(@"XXXXXX   \IN");
FileInfo[] files = directory.GetFiles("*.*");

     foreach (var f in files)   //FETCHING FILES FROM THE BULK FOLDER (IN)
        {
            string path_f = f.FullName;
            StreamReader myfile = new StreamReader(path_f);                    
            StreamReader basis = new StreamReader(@"C:XXXXXXXX\SCH.TXT");

               while (basis.EndOfStream == false)
                        {
                            string canon = basis.ReadLine();  //CANON STORES EACH WORD FROM SCH.TXT
                            canon = canon.Trim();

                            while (myfile.EndOfStream == false)
                            {
                                string line = myfile.ReadLine();
                                if (line.Contains(canon))
                                DISCH_COUNT++;


                            } // END OF WHILE

                         } // END OF WHILE

                    basis.Close();               

            } 

        myfile.close();

SCH.TXT contains some words. I need to search how many times these words appeared in the a file. I am picking up each word from the SCH.txt and searching in the file.My issue is for the first word, the search is done finley. From the second word onwards the condition myfile.EndOfStream == false is getting failed. Can you please help me regarding this..??

METALHEAD
  • 2,734
  • 3
  • 22
  • 37
  • Did you try to debug your code? Also see [mcve]. – PJvG Apr 05 '17 at 08:53
  • 1
    Depending on the number of words in "basis", you would probably be better reading all of those words into a list & then trying to match each of those words in that list to each line read in the individual files. At the moment you read the "basis" file for every file you are comparing - unless "basis" can change that is unnecessary. And then you read each of the input files once per word of the "basis" file - again unnecessary. – PaulF Apr 05 '17 at 08:56
  • Yes....the condition `myfile.EndOfStream == false` is getting skipped after the first word search is done. How to make the search to be done from the beginning of the file again..?? – METALHEAD Apr 05 '17 at 08:57
  • http://stackoverflow.com/questions/30280142/word-frequency-in-a-large-text-file – Pedro Perez Apr 05 '17 at 09:03
  • @METALHEAD did you have a look at the duplicate i posted? You have to return your `myfile` Stream to the beginning. – Pikoh Apr 05 '17 at 09:05
  • That solved my issue..... Thank you so much @Pikoh – METALHEAD Apr 05 '17 at 09:10

1 Answers1

0

try this for reading text content line by line

int counter = 0;
string line;

// Read the file and display it line by line.
System.IO.StreamReader file = 
   new System.IO.StreamReader("c:\\test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}

file.Close();

I haven't test the code, but it will give you the basic idea that how can you achieve the search functionality. Good luck

R K Sharma
  • 845
  • 8
  • 23
  • 42