Harry Potter and the Sorcerer's Stone


BinaryWriter yordamida obyektni ikkilik faylga yozish uchun C# usulini yozing


Download 192.49 Kb.
bet20/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   16   17   18   19   20   21   22   23   ...   62
Bog'liq
Algaritim amaliy-2

38. BinaryWriter yordamida obyektni ikkilik faylga yozish uchun C# usulini yozing.
Javob:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public void WriteObjectToBinaryFile(YourObject obj, string binFilePath)
{
using (BinaryWriter writer = new BinaryWriter(File.Open(binFilePath, FileMode.Create)))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(writer.BaseStream, obj);
}
}
Yana, iltimos, kod parchasidagi YourObject ni ikkilik maʼlumotlaringiz uchun foydalanayotgan haqiqiy sinf/tur bilan almashtiring.
39. BinaryReader va BinaryWriter yordamida ikkilik faylga ma'lumotlarni o'qish va yozish uchun C# dasturini amalga oshiring va uni C# ob'ektiga aylantiring.
Javob:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
string binFilePath = "path/to/binary/file.bin";
YourObject obj;
// Write the data to the binary file
using (BinaryWriter writer = new BinaryWriter(File.Open(binFilePath, FileMode.Create)))
{
// Write some data to the binary file
writer.Write(42);
writer.Write("Hello, world!");
// ...and so on

// Flush and dispose


writer.Flush();
writer.Close();
}
// Read the data from the binary file and convert it to an object
using (BinaryReader reader = new BinaryReader(File.Open(binFilePath, FileMode.Open)))
{
// Read the data from the binary file
int num = reader.ReadInt32();
string message = reader.ReadString();
// ...and so on

// Create and populate the object


obj = new YourObject();
obj.NumProperty = num;
obj.StringProperty = message;
// ...and so on
}
Iltimos, kod parchasidagi YourObject ni haqiqiy sinf/turingiz bilan almashtiring.
40. C# da CSV faylini o'qish va uni C# obyektiga aylantirish uchun TextFieldParser dan foydalaning.
Javob:
using Microsoft.VisualBasic.FileIO;
string filePath = "path/to/csv/file.csv";
List objects = new List();

using (TextFieldParser parser = new TextFieldParser(filePath))


{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");

// Read the header row, if necessary


// string[] header = parser.ReadFields();

while (!parser.EndOfData)


{
string[] fields = parser.ReadFields();

// Create a new object and populate its properties


YourObject obj = new YourObject();
obj.Property1 = fields[0];
obj.Property2 = fields[1];
// ...and so on

objects.Add(obj);


}
}
Yana, iltimos, kod parchasidagi YourObject ni haqiqiy sinfingiz/turingiz bilan almashtiring va CSV faylingiz uchun kerak boʻlganda chegaralovchi va boshqa sozlamalarni sozlang.

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   16   17   18   19   20   21   22   23   ...   62




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