Как организовать поиск в файле и вывод в другой файл? - C#
Формулировка задачи:
В файл записать информацию о сотрудниках некоторого предприятия: фамилия, домашний адрес, телефон, образование, оклад. Напечатать в другой файл список сотрудников, имеющих высшее образование.
Ввод информации в файл сделал, но не получается организовать поиск и вывод в другой файл.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- namespace Excellently_Console_
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n;
- Console.WriteLine("Введите количество сотрудников");
- n = Convert.ToInt32(Console.ReadLine());
- string[] Text = new string[n];
- Console.WriteLine("Введите фамилию, домашний адрес, телефон, образование, оклад(через пробел):");
- for (int i = 0; i < n; i++)
- {
- Text[i] = Console.ReadLine();
- File.AppendAllText(@"D:\FILE.txt", Text[i], Encoding.GetEncoding(1251));
- File.AppendAllText(@"D:\FILE.txt", "\r\n");
- }
- }
- }
- }
Решение задачи: «Как организовать поиск в файле и вывод в другой файл?»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Drawing;
- using System.IO;
- namespace MyPractice
- {
- class Program
- {
- public struct Personal
- {
- public string sorName;
- public string adress;
- public string phoneNumber;
- public string education;
- public double salary;
- public Personal ( string sorName,string adress,string phoneNumber,
- string education, double salary )
- {
- this.sorName= sorName;
- this.adress=adress;
- this.phoneNumber = phoneNumber;
- this.education = education;
- this.salary = salary;
- }
- public override string ToString()
- {
- return sorName+","+adress+","+phoneNumber+","+education+","+salary;
- }
- }
- public static Personal InputPerson()
- {
- Personal person= new Personal ();
- Console.WriteLine("Input SorName: ");
- person.sorName= Console.ReadLine();
- Console.WriteLine("Input Adress: ");
- person.adress = Console.ReadLine();
- Console.WriteLine("Input PhoneNumber: ");
- person.phoneNumber = Console.ReadLine();
- Console.WriteLine("Input Education: ");
- person.education = Console.ReadLine();
- Console.WriteLine("Input salary: ");
- person.salary = double.Parse(Console.ReadLine());
- return person;
- }
- public static List<Personal> Input()
- {
- bool z = true;
- List<Personal> myList = new List<Personal>();
- while (z)
- {
- Console.Clear();
- Console.WriteLine("Input person press 1");
- Console.WriteLine("Exit press 2");
- string str = Console.ReadLine();
- switch (str)
- {
- case "1": { Console.Clear(); myList.Add(InputPerson()); break; }
- case "2": { Console.Clear(); z = false; break; }
- default:
- {
- Console.Clear(); Console.WriteLine("Make your choice correctly!!!");
- break;
- }
- }
- }
- return myList;
- }
- public static void FileWrite(List<Personal> myList)
- {
- using (StreamWriter sw = new StreamWriter(@"d:\myFile.txt"))
- {
- foreach (var i in myList)
- {
- sw.WriteLine(i);
- }
- }
- }
- public static List<Personal> SearchAndWrite(List<Personal> myList)
- {
- List<Personal> myList1 = new List<Personal>();
- foreach (var i in myList)
- {
- if (i.education == "высшее")
- {
- myList1.Add(i);
- }
- }
- return myList1;
- }
- public static void NewFileWrite(List<Personal> myList1)
- {
- using (StreamWriter sw = new StreamWriter(@"d:\myFile1.txt"))
- {
- foreach (var i in myList1)
- {
- sw.WriteLine(i);
- }
- }
- }
- static void Main(string[] args)
- {
- List<Personal> myList=Input();
- foreach ( var i in myList)
- {
- Console.WriteLine(i);
- }
- FileWrite(myList);
- List<Personal> myList1 = SearchAndWrite(myList);
- NewFileWrite(myList1);
- Console.ReadKey();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д