Prev: C# 2008 .net framework 3.5 Windows form CrystalReport question
Next: Hidden column is visible on reload
From: nicol on 21 May 2010 03:55 hi i want to write some number in a file & than read second number( i mean in just want to read one number from the file )i wrote a code for it but it does not work i want second number but it writes for me the first number here is it codes using System; using System.IO; class Program { static void Main(string[] args) { int a = 7, b = 2; TextWriter write = new StreamWriter("data6.txt"); write.WriteLine(a); write.WriteLine(b); write.Close(); TextReader read = new StreamReader("data6.txt"); Console.WriteLine(read.ReadLine()); read.Close(); } }
From: Anders Eriksson on 21 May 2010 05:29 Hello, "nicol" <nicol.young20(a)gmail.com> skrev i meddelandet news:9b373147-422e-4a7c-8a0e-56f33796d93a(a)z13g2000prh.googlegroups.com... > hi > i want to write some number in a file & than read second number( i > mean in just want to read one number from the file )i wrote a code for > it but it does not work i want second number but it writes for me the > first number > here is it codes > > > using System; > using System.IO; > > class Program > { > static void Main(string[] args) > { > int a = 7, b = 2; > TextWriter write = new StreamWriter("data6.txt"); > write.WriteLine(a); > write.WriteLine(b); > write.Close(); > TextReader read = new StreamReader("data6.txt"); > Console.WriteLine(read.ReadLine()); > read.Close(); > } > } Since TextReader is reading the file sequencially you need to read twice to get the second line. Just add a read.ReadLine() before the Console.WriteLine(..) // Anders
From: nicol on 21 May 2010 10:30
On May 21, 1:29 pm, "Anders Eriksson" <andis.eriks...(a)gmail.com> wrote: > Hello, > > "nicol" <nicol.youn...(a)gmail.com> skrev i meddelandetnews:9b373147-422e-4a7c-8a0e-56f33796d93a(a)z13g2000prh.googlegroups.com... > > > > > hi > > i want to write some number in a file & than read second number( i > > mean in just want to read one number from the file )i wrote a code for > > it but it does not work i want second number but it writes for me the > > first number > > here is it codes > > > using System; > > using System.IO; > > > class Program > > { > > static void Main(string[] args) > > { > > int a = 7, b = 2; > > TextWriter write = new StreamWriter("data6.txt"); > > write.WriteLine(a); > > write.WriteLine(b); > > write.Close(); > > TextReader read = new StreamReader("data6.txt"); > > Console.WriteLine(read.ReadLine()); > > read.Close(); > > } > > } > > Since TextReader is reading the file sequencially you need to read twice to > get the second line. Just add a read.ReadLine() before the > Console.WriteLine(..) > > // Anders thanks but there is no other way? |