file [?]

N

nicol

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

Anders Eriksson

Hello,

nicol said:
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
 
N

nicol

Hello,

"nicol" <[email protected]> skrev i meddelandet

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top