Работа с двоичными файлами: слова заданной длины - C#
Формулировка задачи:
Работа с двоичными файлами: Создать файл, состоящий из слов. Вывести на экран все слова, длина которых равна заданному числу.
Решение задачи: «Работа с двоичными файлами: слова заданной длины»
textual
Листинг программы
int countwords;
up:
try
{
Console.SetCursorPosition(0, 0);
Console.Write("Сколько слов вы хотите ввести? : ");
string t = Console.ReadLine();
countwords = Convert.ToInt32(t);
}
catch
{
Console.Clear();
goto up;
}
string line ="";
for (int x = 0; x < countwords; x++)
{
Console.Clear();
Console.Write("Введите слово : ");
line += Console.ReadLine() + " ";
}
using (BinaryWriter writer = new BinaryWriter(File.Open(@"С:\Temp\text.bin", FileMode.OpenOrCreate)))
{
writer.Write(line);
}
Console.Clear();
Console.WriteLine("Ваши слова были записаны в фаил С:\Temp\text.bin");
Console.WriteLine("Нажмите любую клавишу! :");
Console.ReadKey();
Console.Clear();
int lineword = 0;
up1:
try
{
Console.SetCursorPosition(0, 0);
Console.Write("Введите длину слова :");
string t = Console.ReadLine();
lineword = Convert.ToInt32(t);
}
catch
{
Console.Clear();
goto up1;
}
using (BinaryReader reader = new BinaryReader(File.Open(@"С:\Temp\text.bin", FileMode.Open)))
{
while (reader.PeekChar() > -1)
{
line = reader.ReadString();
}
}
int countIndex = 0;
string text = "";
foreach (char ch in line)
{
if (ch == ' ')
{
if (countIndex <= lineword)
{
Console.Write(text + " ");
}
countIndex = 0;
text = "";
}
else
{
text += ch;
countIndex++;
}
}