C++ dasturlash tilida ma’lumotlarning faylli toifasi


Download 21.78 Kb.
bet3/4
Sana02.01.2022
Hajmi21.78 Kb.
#191742
1   2   3   4
Bog'liq
22-variant

3- misol. fayldan ma’lumotlarni bitta bitta belgi orqali o’qish va chiqarish.

class Program {

[STAThread]

static void Main(string[] args) {

StreamReader uqish;

try


{

uqish = new StreamReader("d:\\textFile.txt");

}

catch {


Console.WriteLine("Faylni ochishda xatolik bor");

Console.ReadKey();

return; }

int ch;


while ((ch = uqish.Read()) != (-1))

{

Console.WriteLine((char)ch);



}

Console.ReadKey();

} }

4- misol. Faylda ma’lumotlarni binar yozish va o’qish.

class Program {

static void Main(string[] args) {

FileStream a = new FileStream("d:\\a.txt", FileMode.CreateNew);

BinaryWriter w = new BinaryWriter(a);

//faylga ma'lumotlarni yozamiz

for (int i = 1; i < 20; i++) {

w.Write( (int) i); }

//faylni yopish

w.Close();

a = new FileStream("d:\\a.txt", FileMode.Open, FileAccess.Read);

BinaryReader r = new BinaryReader(a);

// fayldan ma’lumotlarni o’qiymiz

for (int i = 1; i < 20; i++) {

Console.WriteLine(r.ReadInt32()); }

r.Close();

Console.ReadKey();

} }


5- misol. fayldan ma’lumotlarni satrma - satr o’qish.

class Program {

static void Main(string[] args){

FileStream a = new FileStream("d:\\textFile.txt", FileMode.Open, FileAccess.Read);

StreamReader b = new StreamReader(a);

string s;

while ((s = b.ReadLine()) != null){

Console.WriteLine(s); }

b.Close();

Console.ReadKey();

} }


Download 21.78 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




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