Advertisement
petromaxa

Untitled

Jan 31st, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Security;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace _13.ScanFileForListOfWords
  10. {
  11. class ScanFileForListOfWords
  12. {
  13. static void Main()
  14. {
  15. try
  16. {
  17. StreamReader wordsReader = new StreamReader("words.txt");
  18. using (wordsReader)
  19. {
  20. string wordsFileContent = wordsReader.ReadToEnd();
  21. string[] words = wordsFileContent.Split(new string[] { "\r\n", " " }, StringSplitOptions.RemoveEmptyEntries);
  22. int[] numberOfMatches = new int[words.Length];
  23. for (int i = 0; i < words.Length; i++)
  24. {
  25. StreamReader sourceFileReader = new StreamReader("test.txt");
  26. using (sourceFileReader)
  27. {
  28. string newLine = sourceFileReader.ReadLine();
  29. while (newLine != null)
  30. {
  31. string[] wordsInLine = newLine.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
  32. for (int wordIndex = 0; wordIndex < wordsInLine.Length; wordIndex++)
  33. {
  34. if (wordsInLine[wordIndex] == words[i])
  35. {
  36. numberOfMatches[i]++;
  37. }
  38. }
  39. newLine = sourceFileReader.ReadLine();
  40. }
  41. }
  42. }
  43. Array.Sort(numberOfMatches, words);
  44. Array.Reverse(numberOfMatches);
  45. Array.Reverse(words);
  46. StreamWriter writer = new StreamWriter("result.txt");
  47. using (writer)
  48. {
  49. for (int i = 0; i < words.Length; i++)
  50. {
  51. writer.WriteLine("{0} - {1}", words[i], numberOfMatches[i]);
  52. }
  53. }
  54. }
  55. }
  56. catch (FileNotFoundException)
  57. {
  58. Console.WriteLine("The specified path is invalid! There is no such file in this directory!");
  59. }
  60. catch (IOException)
  61. {
  62. Console.WriteLine("An I/O error occurred during file read/write opperations!");
  63. }
  64. catch (OutOfMemoryException)
  65. {
  66. Console.WriteLine("There is insufficient memory to allocate a buffer for the returned string!");
  67. }
  68. catch (UnauthorizedAccessException)
  69. {
  70. Console.WriteLine("Access is denied and writting in the file is not allowed!");
  71. }
  72. catch (PlatformNotSupportedException)
  73. {
  74. Console.WriteLine("The operating system is Windows 98 Second Edition or earlier and the files system is not NTFS!");
  75. }
  76. catch (SecurityException)
  77. {
  78. Console.WriteLine("The caller does not have the required permission to write in the file!");
  79. }
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement