Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Security;
- using System.Text.RegularExpressions;
- namespace _13.ScanFileForListOfWords
- {
- class ScanFileForListOfWords
- {
- static void Main()
- {
- try
- {
- StreamReader wordsReader = new StreamReader("words.txt");
- using (wordsReader)
- {
- string wordsFileContent = wordsReader.ReadToEnd();
- string[] words = wordsFileContent.Split(new string[] { "\r\n", " " }, StringSplitOptions.RemoveEmptyEntries);
- int[] numberOfMatches = new int[words.Length];
- for (int i = 0; i < words.Length; i++)
- {
- StreamReader sourceFileReader = new StreamReader("test.txt");
- using (sourceFileReader)
- {
- string newLine = sourceFileReader.ReadLine();
- while (newLine != null)
- {
- string[] wordsInLine = newLine.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
- for (int wordIndex = 0; wordIndex < wordsInLine.Length; wordIndex++)
- {
- if (wordsInLine[wordIndex] == words[i])
- {
- numberOfMatches[i]++;
- }
- }
- newLine = sourceFileReader.ReadLine();
- }
- }
- }
- Array.Sort(numberOfMatches, words);
- Array.Reverse(numberOfMatches);
- Array.Reverse(words);
- StreamWriter writer = new StreamWriter("result.txt");
- using (writer)
- {
- for (int i = 0; i < words.Length; i++)
- {
- writer.WriteLine("{0} - {1}", words[i], numberOfMatches[i]);
- }
- }
- }
- }
- catch (FileNotFoundException)
- {
- Console.WriteLine("The specified path is invalid! There is no such file in this directory!");
- }
- catch (IOException)
- {
- Console.WriteLine("An I/O error occurred during file read/write opperations!");
- }
- catch (OutOfMemoryException)
- {
- Console.WriteLine("There is insufficient memory to allocate a buffer for the returned string!");
- }
- catch (UnauthorizedAccessException)
- {
- Console.WriteLine("Access is denied and writting in the file is not allowed!");
- }
- catch (PlatformNotSupportedException)
- {
- Console.WriteLine("The operating system is Windows 98 Second Edition or earlier and the files system is not NTFS!");
- }
- catch (SecurityException)
- {
- Console.WriteLine("The caller does not have the required permission to write in the file!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement