Valhalla Legends Forums Archive | .NET Platform | [C#] Reading file

AuthorMessageTime
Fr0z3N
This should read from a simple text file right?

[code]
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());
}
}
}
[/code]

Edit: If your woundering I don't have a compiler at my dads house therefor cannot test it.
January 11, 2004, 5:28 PM
K
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?

[code]
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());
      }
   }
}
[/code]
January 12, 2004, 6:27 AM
Fr0z3N
Thanks K.
January 12, 2004, 1:12 PM

Search