BinaryReader va BinaryWriter yordamida ikkilik faylga ma'lumotlarni o'qish va yozish uchun C# dasturini amalga oshiring va uni C# ob'ektiga aylantiring.
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Fayl nomi
string fileName = "example.bin";
// Faylga yozish
using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
{
writer.Write(42);
writer.Write("Hello, world!");
writer.Write(3.14159);
}
// Fayldan o'qish
using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
{
int intValue = reader.ReadInt32();
string stringValue = reader.ReadString();
double doubleValue = reader.ReadDouble();
// O'qilgan ma'lumotlarni konsolga chiqarish
Console.WriteLine("int qiymati: " + intValue);
Console.WriteLine("string qiymati: " + stringValue);
Console.WriteLine("double qiymati: " + doubleValue);
}
}
}
69. Oddiy dastur o'rniga siqilgan ma'lumotlar oqimidan foydalanish uchun mavjud C# dasturini o'zgartiring.
using System;
class Program
{
static void Main(string[] args)
{
int sum = 0;
string input;
Console.WriteLine("Son kiriting (to'xtatish uchun 'q' tugmasini bosing):");
while ((input = Console.ReadLine()) != "q")
{
if (int.TryParse(input, out int num))
{
sum += num;
}
else
{
Console.WriteLine("Noto'g'ri format! Iltimos, son kiriting.");
}
}
Console.WriteLine("Sonlar yig'indisi: " + sum);
}
}
Do'stlaringiz bilan baham: |