Работа с текстовым файлом - C# (181904)
Формулировка задачи:
Ребята, что не так с записью и чтением? Как сделать, чтобы нормально проходили запись и чтение? А то в файл пишется все время "app.Guests" и ничего не читается.
using System;
using System.IO;
namespace app
{
class Hotel
{
string name;
int nofr;
int sofr;
string cofr;
public string Name { get { return name; } set { name = value; } }
public int Nofr { get { return nofr; } set { nofr = value; } }
public int Sofr { get { return sofr; } set { sofr = value; } }
public string Cofr { get { return cofr; } set { cofr = value; } }
}
class Guests : Hotel
{
public string Fio { get; set; }
}
class Methods
{
public static void Write()
{
Guests obj = new Guests();
Console.WriteLine("Введите название файла: ");
string path = Console.ReadLine();
using (StreamWriter sw = File.CreateText(path))
{
Console.WriteLine("Введите название отеля: ");
obj.Name = Console.ReadLine();
Console.WriteLine("Введите количество номеров: ");
int numap = int.Parse(Console.ReadLine());
Console.WriteLine("Введите количество посетителей: ");
int n = int.Parse(Console.ReadLine());
if (n > numap)
Console.WriteLine("Количество посетителей больше количества номеров!");
for (int i = 0; i < n; i++)
{
Console.Write("Введите ФИО посетителя {0}: ", i + 1);
obj.Fio = Console.ReadLine();
Console.Write("Введите номер комнаты: ");
obj.Nofr = int.Parse(Console.ReadLine());
Console.Write("Введите площадь комнаты: ");
obj.Sofr = int.Parse(Console.ReadLine());
Console.Write("Введите класс номера: ");
obj.Cofr = Console.ReadLine();
sw.WriteLine(obj);
}
}
}
public static void Read()
{
Guests obj = new Guests();
Console.WriteLine("Введите название файла: ");
string path = Console.ReadLine();
using (StreamReader sr = File.OpenText(path))
{
string s;
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine("\tОтель {0}", obj.Name);
Console.WriteLine("\nФИО: {0}\nНомер комнаты: {1}\nПлощадь комнаты: {2}\nКласс номера: {3}", obj.Fio, obj.Nofr, obj.Sofr, obj.Cofr);
Console.WriteLine("---------------");
}
}
}
public static void Lofr()
{
Guests obj = new Guests();
Console.WriteLine("Введите название файла: ");
string path = Console.ReadLine();
using (StreamReader sr = File.OpenText(path))
{
string s;
Console.WriteLine("\tЗанятые номера: ");
Console.WriteLine("--------------------");
while ((s = sr.ReadLine()) != null)
{
Console.Write("{0}; ", obj.Nofr);
}
Console.WriteLine("\n--------------------");
}
}
}
class Program
{
static void Main()
{
while (true)
{
Console.Write("\n 1 - Записать в файл\n 2 - Прочитать из файла\n" +
" 3 - Список занятых номеров\n 4 - Выход\n");
switch (Console.ReadLine())
{
case "1": Methods.Write(); break;
case "2": Methods.Read(); break;
case "3": Methods.Lofr(); break;
case "4": return;
}
}
}
}
}Решение задачи: «Работа с текстовым файлом»
textual
Листинг программы
public static void Read() // чтение из файла
{
Console.WriteLine("Введите название файла: ");
string path = Console.ReadLine();
using (StreamReader sr = new StreamReader(path)) // открываем и читаем файл
{
string s;
while ((s = sr.ReadToEnd()) != null)
{
Console.Write(s);
break;
}
}
}