Harry Potter and the Sorcerer's Stone


Ma'lumotlar oqimi yordamida CSV faylidan ma'lumotlarni o'qiydigan va uni formatlangan tarzda ko'rsatadigan C# dasturini yozing


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

44. Ma'lumotlar oqimi yordamida CSV faylidan ma'lumotlarni o'qiydigan va uni formatlangan tarzda ko'rsatadigan C# dasturini yozing.
Javob:
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\example.csv";

try
{


using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] fields = line.Split(",");
Console.WriteLine("{0,-10} {1,-10} {2,-10}", fields[0], fields[1], fields[2]);
}
}
}
catch (IOException e)
{
Console.WriteLine("An error occurred while reading the file: " + e.Message);
}

Console.ReadKey();


}
}
}
45. Ma'lumotlar oqimi yordamida ikkilik faylni o'qiydigan va uning mazmunini ko'rsatadigan C# dasturini amalga oshiring.
Javob:
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\example.bin";

try
{


using (BinaryReader reader = new BinaryReader(File.Open(filePath, FileMode.Open)))
{
int number = reader.ReadInt32();
string text = reader.ReadString();
Console.WriteLine("Number: " + number);
Console.WriteLine("Text: " + text);
}
}
catch (IOException e)
{
Console.WriteLine("An error occurred while reading the file: " + e.Message);
}

Console.ReadKey();


}
}
}
46. Ma'lumotlar oqimi yordamida JSON faylidan ma'lumotlarni o'qiydigan va uni formatlangan ko'rinishda ko'rsatadigan C# dasturini yozing.
Javob:
using System;
using System.IO;
using Newtonsoft.Json;
namespace JsonReader
{
class Program
{
static void Main(string[] args)
{
// Read JSON data from a file
string json = File.ReadAllText(@"C:\Users\User\Documents\data.json");

// Deserialize the JSON data into an object


var data = JsonConvert.DeserializeObject(json);

// Display the formatted view


Console.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));
}
}
}

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   18   19   20   21   22   23   24   25   ...   62




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