Harry Potter and the Sorcerer's Stone


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


Download 192.49 Kb.
bet16/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   12   13   14   15   16   17   18   19   ...   62
Bog'liq
Algaritim amaliy-2

28. BinaryReader va BinaryWriter yordamida ikkilik faylga ma'lumotlarni o'qish va yozish uchun C# dasturini amalga oshiring.
Javob:
using System;
using System.IO;
class Program {
static void Main(string[] args) {
// Create a file to write the data to
using (BinaryWriter writer = new BinaryWriter(File.Open("data.bin", FileMode.Create))) {
// Write some sample data to the file
writer.Write(42);
writer.Write(3.14);
writer.Write("Hello, world!");
}

// Read the data back from the file


using (BinaryReader reader = new BinaryReader(File.Open("data.bin", FileMode.Open))) {
int i = reader.ReadInt32();
double d = reader.ReadDouble();
string s = reader.ReadString();

Console.WriteLine($"i = {i}, d = {d}, s = \"{s}\"");


}
}
}
Ushbu dastur "data.bin" deb nomlangan ikkilik faylni yaratadi va unga ba'zi namunali ma'lumotlarni yozadi: butun son, ikkilik va satr. Keyin u maʼlumotlarni fayldan qayta oʻqiydi va konsolda koʻrsatadi.
E'tibor bering, fayldan ma'lumotlarni qayta o'qiyotganda, biz uni faylga yozilgan tartibda o'qishimiz kerak. Shuningdek, oqimlar bilan ishlashda ular bilan ishlash tugagach, ular toʻgʻri yopilganligiga ishonch hosil qilish uchun har doim using iborasidan foydalanishimiz kerak.
29. C# da XML faylini o'qish va uni C# ob'ektiga aylantirish uchun XmlReader dasturidan foydalaning.
Javob:
Bizda "books.xml" deb nomlangan XML fayli bor, deylik, u quyidagicha ko'rinadi:



Harry Potter and the Sorcerer's Stone
Fantasy
1997


The Hobbit
Fantasy
1937


Va biz uni ushbu kitoblarni ifodalovchi C# ob'ektlar ro'yxatiga aylantirmoqchimiz:
using System;
using System.Collections.Generic;
using System.Xml;
class Book {
public string Author { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public int Year { get; set; }
}
class Program {
static void Main(string[] args) {
List books = new List();
using (XmlReader reader = XmlReader.Create("books.xml")) {
while (reader.Read()) {
if (reader.IsStartElement() && reader.Name == "book") {
Book book = new Book();
book.Author = reader.GetAttribute("author");
reader.ReadToDescendant("title");
book.Title = reader.ReadElementContentAsString();
reader.ReadToFollowing("genre");
book.Genre = reader.ReadElementContentAsString();
reader.ReadToFollowing("year");
book.Year = reader.ReadElementContentAsInt();
books.Add(book);
}
}
}
foreach (Book book in books) {
Console.WriteLine($"{book.Title} by {book.Author} ({book.Year}) - {book.Genre}");
}
}
}
Ushbu dastur XmlReader yordamida "books.xml" deb nomlangan XML faylni o'qish orqali Kitob ob'ektlari ro'yxatini yaratadi.
E'tibor bering, ReadElementContentAsString va ReadElementContentAsInt yordamida elementlarning qiymatlarini o'qiyotganda, ReadToDescendant va ReadToFollowing usullari.

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   12   13   14   15   16   17   18   19   ...   62




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