4- misol. Faylda ma’lumotlarni binar yozish va o’qizh. 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();
} }
6- misol. a.txt fayldagi barcha sonlarni yig’indisini b.txt faylda chiqarish.
class Program
{
static void Main(string[] args)
{
StreamWriter yozish = new StreamWriter("d:\\a.txt"); StreamReader uqish = new StreamReader("d:\\b.txt"); int b, n, k,s=0;
string a;
while ((a = (uqish.ReadLine())) != null)
{
s = s + int.Parse(a);
}
yozish.WriteLine(s+""); yozish.Close(); Console.ReadKey();
} }
7- misol. a.txt fayldagi eng uzun so’zni b.txt faylda chiqarish. class Program
{
static void Main(string[] args)
{
StreamWriter yozish = new StreamWriter("d:\\a.txt"); StreamReader uqish = new StreamReader("d:\\b.txt"); int b, n, k,s=0;
string a,x,y; k:=0; x:= "";
while ((a = (uqish.ReadLine())) != null)
{if (a.length()>k) {x = a; k=a.length();}} yozish.WriteLine(x);
yozish.Close(); Console.ReadKey();
} }
Dostları ilə paylaş: |