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));
}
}
}
Do'stlaringiz bilan baham: |