C# da mavjud faylga matnni qanday qo'shish mumkin?


BinaryReader va BinaryWriter yordamida ikkilik faylga ma'lumotlarni o'qish va yozish uchun C# dasturini amalga oshiring va uni C# ob'ektiga aylantiring


Download 22.09 Kb.
bet3/3
Sana17.06.2023
Hajmi22.09 Kb.
#1526406
1   2   3
Bog'liq
Azizbek

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);


}
}
Download 22.09 Kb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling