I am writing this program which I create from the console screen. I want to change it so that I can read any file from the folder.

For example: in this program, I am prompting the console for the two files and comparing them. Instead, I want to compare files that are already in the folder

I want to type IgnoreKwic firstFile secondFile and it should compare the firstFile and secondFile that are already saved in the folder.

Here is my code:

using System;
using System.Collections.Generic;// Adds definition of List<T>
using System.IO; // Adds file I/O definitions
using System.Text;  // Adds StringBuilder definition.

public class IgnoreKwic
{
    static void Main()
    {
        string textPath = "Lines.txt";
        string ignoredPath = "IgnoredWords.txt";

        GetTextLines(textPath);
        GetIgnoredWords(ignoredPath);
        string output = BuildOutput(ignoredPath, textPath);

        // *** Now write the output to the screen.


        Console.WriteLine(output);
    }

    static void GetTextLines(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        string fileLine;
        Console.Write("Please enter the text to be examined ");
        fileLine = Console.ReadLine();
        while (fileLine.Length > 0)
        {
            sw.WriteLine(fileLine);
            fileLine = Console.ReadLine();
        }
        sw.Flush();
        sw.Close();
    }
    static void GetIgnoredWords(string path)
    {
        StreamWriter sw = new StreamWriter(path);
        Console.Write("Please enter the words to be ignored. Please enter words in single line seperated by spaces ");

        string wordList = Console.ReadLine();
        string[] words = wordList.Split(' ');
        for (int i = 0; i < words.Length; i++)
        {
            sw.WriteLine(words[i]);
        }
        sw.Flush();
        sw.Close();
    }

    static string BuildOutput(string ignorePath, string inputPath)
    {
        StringBuilder sb = new StringBuilder(1024);
        StreamReader sr = new StreamReader(ignorePath);

        List<string> ignoredWords = new List<string>();
        string text;
        while ((text = sr.ReadLine()) != null)
        {
            ignoredWords.Add(text);
        }

        StreamReader sr2 = new StreamReader(inputPath);
        List<string> inputs = new List<string>();

        while ((text = sr2.ReadLine()) != null)
        {
            inputs.Add(text);
        }

 // Get the index words from the set of inputs
         List<string> indexWords = FindAllWordsForCaps(inputs, ignoredWords);
         List<string> currentLines = null;
         List<int> offsets = null;
         int maxOffset;
         string pad = string.Empty;

         foreach (string indexWord in indexWords)
         {
            maxOffset = -1;
            currentLines = GetAllLinesForIndexWord(inputs, indexWord, ref offsets);
            for (int i = 0; i < offsets.Count; i++)
            {
               if (offsets[i] > maxOffset)
                  maxOffset = offsets[i];
            }
            for (int j = 0; j < currentLines.Count; j++)
            {
               pad = new string(' ', maxOffset - offsets[j]);
               sb.Append(pad);
               if (offsets[j] > 0)
               {
                  sb.Append(currentLines[j].Substring(0, offsets[j]));
               }
               sb.Append(currentLines[j].Substring(offsets[j], indexWord.Length).ToUpper());
               if (currentLines[j].Length > (offsets[j] + indexWord.Length))
               {
                  sb.Append(currentLines[j].Substring(offsets[j] + indexWord.Length));
               }
               sb.Append("\n");
            }
         }
         return sb.ToString();
      }
      private static List<string> GetAllLinesForIndexWord(List<string> inputLines, string indexWord, ref List<int> offsets)
      {
         List<string> output = new List<string>(10);
         offsets = new List<int>(10);
         string lowIndex = indexWord.ToLower();
         int pos = 0;

         foreach (string line in inputLines)
         {
            pos = (line.ToLower()).IndexOf(lowIndex);
            if (pos > -1)
            {
               output.Add(line);
               offsets.Add(pos);
            }
         }
         return output;
      }
      private static List<string> FindAllWordsForCaps(List<string> inputLines, List<string> ignoredWords)
      {
         List<string> indexWords = new List<string>(64);
         string[] allCurrent = null;
         string current;
         int currentIndex = 0;
         int inputIndex = 0;
         int ignoredIndex = 0;
         string actualCurrent = string.Empty;

         while (inputIndex < inputLines.Count)
         {
            allCurrent = inputLines[inputIndex++].Split(' ');
            currentIndex = 0;
            while (currentIndex < allCurrent.Length)
            {
               current = allCurrent[currentIndex++];
               actualCurrent = current.ToLower();
               if (current.EndsWith(","))
                  current = current.Substring(0, current.Length - 1);
               ignoredIndex = 0;
               while (ignoredIndex < ignoredWords.Count)
               {
                  if (string.Compare(current, ignoredWords[ignoredIndex], true) == 0)
                     break;
                  ignoredIndex++;
               }
               if (ignoredIndex == ignoredWords.Count) // This one gets caps
               {
                  if (!indexWords.Contains(actualCurrent))
                     indexWords.Add(actualCurrent);
               }
            }
         }
         return indexWords;
      }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

i wrote this program so that it takes input from the console and creates files.
First the program puts the message on the console"please enter the text to be examined"

the program then gets the text and saves it as the input file.

then, the program puts another message on the console"please enter the words to be ignored. separate the words by spaces"

The program builds on these two files created by the input in the console. Rather than creating these files, I want the program to build on any files.
If the program is called Kwic and if I type Kwic fileOne fileTwo, the program should take fileOne instead of the inputs from "please enter the text to be examined" and should take fileTwo instead of "please enter the words to be ignored. separate the words by spaces".

I think the program should use FileReader instead of console.ReadLine?
I need a little help in making changes in the GetTextLines() and GetIgnoredWords().

This article has been dead for over six months. Start a new discussion instead.