• Welcome to Valhalla Legends Archive.
 

[C#] Reading file

Started by Fr0z3N, January 11, 2004, 11:28 AM

Previous topic - Next topic

Fr0z3N

This should read from a simple text file right?


using System;
using System.IO;
class MyStream
{
   private const string FILE_NAME = "Test.txt";
   public static void Main(String[] args)
   {
       if (File.Exists(FILE_NAME))
       {
           Console.WriteLine("{0} already exists!", FILE_NAME);
           return;
       }
       FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
       BinaryReader r = new BinaryReader(fs);
       for (int i = 0; i < 11; i++)
       {
           Console.WriteLine(r.ReadInt32());
       }
   }
}


Edit: If your woundering I don't have a compiler at my dads house therefor cannot test it.

K

#1
That's not going to read a text file the way you want it to.  It would read from a binary file.  How about something like this?


using System;
using System.IO;
class MyStream
{
private const string FILE_NAME = "Test.txt";

public static void Main(String[] args)
{
   // we should probably fail if the file DOESNT exist.
   // since we're reading from it
   if (!File.Exists(FILE_NAME))
   {
      Console.WriteLine("file doesn't exists!");
      return;
   }

      FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
      StreamReader s = new StreamReader(fs);
   
      while(s.BaseStream.Position < s.BaseStream.Length)
      {
         Console.WriteLine(s.ReadLine());
      }
   }
}

Fr0z3N