Advertisement
l-kikov

Untitled

Oct 19th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ReadFile
  9. {
  10.     class FromLocalComputer
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             // Fix the encoding to UTF8 if  work with cyrillic characters
  15.             Console.OutputEncoding = Encoding.UTF8;
  16.  
  17.             const string course = "Въведение в програмирането";
  18.  
  19.             // Take the full path to the file
  20.             string file = @"C:\Users\Republic Of Gamers\Desktop\TEMPORARY\students.txt";
  21.  
  22.             // Use ADT (Abstract Data Type) to hold the data from the file
  23.             List<string> studentList = new List<string>();
  24.  
  25.             #region Read the file
  26.             // Create object who will read the file & pass its constructor path to the file
  27.             StreamReader fileReader = new StreamReader(file);
  28.  
  29.             // Open connection for the data stream
  30.             using (fileReader)
  31.             {
  32.                 // Create variable in wich we will hold the line who's going to be read from the file & start reading
  33.                 string currentLine = fileReader.ReadLine();
  34.  
  35.                 // Pass condition in who the file contains only empty lines & use it ti understand when there is no tex to be readed anymore
  36.                 while (currentLine != null)
  37.                 {
  38.                     // If there is a data - add it to the list
  39.                     studentList.Add(currentLine);
  40.  
  41.                     // Print the current line data
  42.                     Console.WriteLine("Student name : {0} \nCourse : {1}", currentLine, course);
  43.                     Console.WriteLine("----------------------------------------");
  44.                     // Read the next line of the file
  45.                     currentLine = fileReader.ReadLine();
  46.                 }
  47.             }
  48.             #endregion END Read the file
  49.  
  50.             #region Print data from the studentList
  51.             Console.WriteLine("888888888888888888888888888888888888888888888888");
  52.             Console.WriteLine();
  53.  
  54.             // See how much students are
  55.             Console.WriteLine("There are {0} students in the group & names are: ", studentList.Count);
  56.             studentList.Sort();
  57.  
  58.             // Create random generator just for fun
  59.             Random rnd = new Random();
  60.  
  61.             foreach (string studentName in studentList)
  62.             {
  63.                 // Generate fake ID
  64.                 int rndNumber = rnd.Next(byte.MinValue, byte.MaxValue);
  65.  
  66.                 // Print the new data
  67.                 Console.WriteLine("{0} \nФакултетен номер: {1}", studentName, rndNumber);
  68.                 Console.WriteLine("..............................................................");
  69.             }
  70.             #endregion
  71.  
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement