Harry Potter and the Sorcerer's Stone


Faylni o'qish va uning mazmunini ma'lumotlar oqimi yordamida ko'rsatish uchun C# dasturini yozing


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

41. Faylni o'qish va uning mazmunini ma'lumotlar oqimi yordamida ko'rsatish uchun C# dasturini yozing.
Javob:
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\example.txt";

try
{


using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (IOException e)
{
Console.WriteLine("An error occurred while reading the file: " + e.Message);
}

Console.ReadKey();


}
}
}
42. Ma'lumotlar oqimi yordamida faylga ma'lumotlarni yozish uchun C# dasturini amalga oshiring.
Javob:
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\example.txt";

try
{


using (StreamWriter writer = new StreamWriter(filePath))
{
writer.WriteLine("Hello, world!");
}
}
catch (IOException e)
{
Console.WriteLine("An error occurred while writing to the file: " + e.Message);
}

Console.ReadKey();


}
}
}
43. Matn oqimi o'rniga ikkilik ma'lumotlar oqimidan foydalanish uchun mavjud C# dasturini o'zgartiring.
Javob:
using System;
using System.IO;
namespace FileIO
{
class Program
{
static void Main(string[] args)
{
string filePath = @"C:\example.bin";

// Write data to binary file


using (BinaryWriter writer = new BinaryWriter(File.Open(filePath, FileMode.Create)))
{
writer.Write(42);
writer.Write("Hello, world!");
}

// Read data from binary file


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);
}

Console.ReadKey();


}
}
}

Download 192.49 Kb.

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




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