472,144 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

Which way is best to read from a file?

Below is two snippets of code from MSDN showing how to read a file. Is one
way preferred over the other and why? Thanks.
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}

using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Open the stream and read it
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
Nov 18 '05 #1
4 2588
Hi,

Both ways are useful depending on what you need to accomplish. The top way
reads the file one line at a time meaning it reads till the line break. If
your file contains lists of names:

Bill
Smith
Mark
Wilson
Brian
Winters

Then reading line by line will work for you. However if your file is
created without using line breaks and instead has fixed length fields, then
the bottom way will let you grab these anywhere in the file instead of
having to iterate through all the lines.

Bill456Smith56789Mark456Wilson6789Brian56Winters78 9

The line above shows a fixed length field file (visualize a space where each
number is). Each first name takes up 7 characters, each last name takes up
10. So if you wanted to get the 3rd first name you would read 7 characters
starting at position 34 (remember streams start at 0). Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"ESPN Lover" <es**@lover.com> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
Below is two snippets of code from MSDN showing how to read a file. Is one
way preferred over the other and why? Thanks.
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}

using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Open the stream and read it
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}

Nov 18 '05 #2
All I'm trying to do is copy the contents of a file (actually an HTML page
on the file system) into a string. Process the string and output a new
file. So the idea is to read, upfront, the entire file's contents and place
it into a string. This is manipulated and then I'll write out a new file.

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:eV****************@TK2MSFTNGP10.phx.gbl...
Hi,

Both ways are useful depending on what you need to accomplish. The top way reads the file one line at a time meaning it reads till the line break. If your file contains lists of names:

Bill
Smith
Mark
Wilson
Brian
Winters

Then reading line by line will work for you. However if your file is
created without using line breaks and instead has fixed length fields, then the bottom way will let you grab these anywhere in the file instead of
having to iterate through all the lines.

Bill456Smith56789Mark456Wilson6789Brian56Winters78 9

The line above shows a fixed length field file (visualize a space where each number is). Each first name takes up 7 characters, each last name takes up 10. So if you wanted to get the 3rd first name you would read 7 characters starting at position 34 (remember streams start at 0). Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"ESPN Lover" <es**@lover.com> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks.
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}

using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Open the stream and read it
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}


Nov 18 '05 #3
Hi,

Then all you have to do is:

sr.BaseStream.Seek(0, SeekOrigin.Begin);
strEntireFile = sr.ReadToEnd();

Put that in the top version of the examples you showed in the place of the
while loop. Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"ESPN Lover" <es**@lover.com> wrote in message
news:OJ**************@tk2msftngp13.phx.gbl...
All I'm trying to do is copy the contents of a file (actually an HTML page
on the file system) into a string. Process the string and output a new
file. So the idea is to read, upfront, the entire file's contents and place it into a string. This is manipulated and then I'll write out a new file.

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:eV****************@TK2MSFTNGP10.phx.gbl...
Hi,

Both ways are useful depending on what you need to accomplish. The top

way
reads the file one line at a time meaning it reads till the line break.

If
your file contains lists of names:

Bill
Smith
Mark
Wilson
Brian
Winters

Then reading line by line will work for you. However if your file is
created without using line breaks and instead has fixed length fields,

then
the bottom way will let you grab these anywhere in the file instead of
having to iterate through all the lines.

Bill456Smith56789Mark456Wilson6789Brian56Winters78 9

The line above shows a fixed length field file (visualize a space where

each
number is). Each first name takes up 7 characters, each last name takes

up
10. So if you wanted to get the 3rd first name you would read 7

characters
starting at position 34 (remember streams start at 0). Good luck! Ken.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight
If you sign up under me and need help, email me.

"ESPN Lover" <es**@lover.com> wrote in message
news:Os**************@TK2MSFTNGP10.phx.gbl...
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks.
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of // the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}

using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";

// Open the stream and read it
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}



Nov 18 '05 #4
Thanks, Ken!! That's what I was after :-)
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi,

Then all you have to do is:

sr.BaseStream.Seek(0, SeekOrigin.Begin);
strEntireFile = sr.ReadToEnd();

Put that in the top version of the examples you showed in the place of the
while loop. Good luck! Ken.

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Paul Bromley | last post: by
7 posts views Thread by Gladen Blackshield | last post: by
4 posts views Thread by TC | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.