Write output of console in text file

Last Reply 12 days ago By dharmendr

Posted 14 days ago

i am having a normal program in which i want to write the output of console application in text file 

 public class SearchDict_charArray
    {

       
        static readonly string textFile = @"D:\Data\Authors.txt";
        static readonly int SIZE = File.ReadAllLines(textFile).Count();
         
        public class TrieNode
        {
            public TrieNode[] Child = new TrieNode[SIZE];

            
            public Boolean leaf;

            
            public TrieNode()
            {
                leaf = false;
                for (int i = 0; i < SIZE; i++)
                    Child[i] = null;
            }
        }

        
        static void insert(TrieNode root, String Key)
        {
            int n = Key.Length;
            TrieNode pChild = root;

            for (int i = 0; i < n; i++)
            {
                int index = Key[i] - 'a';

                if (pChild.Child[index] == null)
                    pChild.Child[index] = new TrieNode();

                pChild = pChild.Child[index];
            }

            
            pChild.leaf = true;
        }

        
        static void searchWord(TrieNode root, Boolean[] Hash,
                                                String str)
        {
            
            if (root.leaf == true)
                Console.WriteLine(str);

            
            for (int K = 0; K < SIZE; K++)
            {
                if (Hash[K] == true && root.Child[K] != null)
                {
                    
                    char c = (char)(K + 'a');

                    searchWord(root.Child[K], Hash, str + c);
                }
            }
        }

        
        static void PrintAllWords(char[] Arr, TrieNode root,int n)
        {
            
            Boolean[] Hash = new Boolean[SIZE];

            for (int i = 0; i < n; i++)
                Hash[Arr[i] - 'a'] = true;

            
            TrieNode pChild = root;

            
            String str = "";

           
            for (int i = 0; i < SIZE; i++)
            {
               
                if (Hash[i] == true && pChild.Child[i] != null)
                {
                    str = str + (char)(i + 'a');
                    searchWord(pChild.Child[i], Hash, str);
                    str = "";
                }
            }
            
        }

        // Driver code  
        public static void Main(String[] args)
        {
         
            String[] Dict= File.ReadAllLines(textFile);
        
            TrieNode root = new TrieNode();

           
            int n = Dict.Length;
            for (int i = 0; i < n; i++)
                insert(root, Dict[i]);
            string array = "acptse";
            char[] arr = array.ToCharArray();
            int N = arr.Length;

            PrintAllWords(arr, root, N);
            
            string path = @"fileout.txt";
            if (!File.Exists(path))
            {
                using(StreamWriter sw = File.CreateText(path))
                {
                    sw.Write()
                }
            }
        }
    }

 

You are viewing reply posted by: dharmendr 12 days ago.