Harry Potter and the Sorcerer's Stone


Mavjud C# dasturini oddiy dastur o'rniga buferlangan matnli ma'lumotlar


Download 192.49 Kb.
bet36/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   32   33   34   35   36   37   38   39   ...   62
Bog'liq
Algaritim amaliy-2

73. Mavjud C# dasturini oddiy dastur o'rniga buferlangan matnli ma'lumotlar
oqimidan foydalanish uchun o'zgartiring.
Javob:
using (var streamReader = new StreamReader(File.OpenRead("file.txt")))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
// do something with each line of data
}
}
Ushbu kod "file.txt" faylining mazmunini satr bo'yicha o'qish uchun StreamReader dasturidan foydalanadi. Foydalanish holatlaringizga qarab uni oʻzgartirishingiz mumkin.
Ikkinchi vazifa uchun, UDP ulanishi orqali ma'lumotlarni yuborish va qabul qilish uchun oqimdan foydalanadigan C# dasturini yaratish uchun siz UdpClient sinfidan foydalanishingiz mumkin.
74. UDP ulanishi orqali ma'lumotlarni yuborish va qabul qilish uchun ma'lumotlar oqimidan foydalanadigan C# dasturini yarating.
Javob:
dpClient udpClient = new UdpClient();
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 12345); // example IP address and port
byte[] dataToSend = Encoding.ASCII.GetBytes("Hello, World!");
udpClient.Send(dataToSend, dataToSend.Length, endPoint);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
byte[] receivedData = udpClient.Receive(ref remoteEP);
string receivedString = Encoding.ASCII.GetString(receivedData);
Ushbu kod UdpClient ob'ektini yaratadi, ma'lumotlarni jo'natish uchun IP-manzil va portni o'rnatadi, bayt massivi sifatida yuboriladigan ma'lumotlarni kodlaydi, ma'lumotlarni yuboradi va keyin masofaviy so'nggi nuqtadan javob oladi.
Fayl tizimi bilan ishlash:
1. C# yordamida yangi matn fayli yarating va unga bir oz matn yozing.
Javob:
string filePath = "C:/example.txt";
string textToWrite = "Hello, world!";
File.WriteAllText(filePath, textToWrite);
2. C# yordamida mavjud matn faylini oching va uning mazmunini o'qing.
Javob:
string filePath = "C:/example.txt";
string text = File.ReadAllText(filePath);
Console.WriteLine(text);

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   32   33   34   35   36   37   38   39   ...   62




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