Click to See Complete Forum and Search --> : read ascii


laasunde
June 2nd, 2005, 09:06 AM
Hello, I'm a C# beginner and have a simple question. I need to read an ascii file which is roughly 640kb - 2Mb and then analyse it, i.e. read it line for line. Can anyone recommend any object\methods for doing such a task.

Links to related sites would be nice.

Thanks :)

mmetzger
June 2nd, 2005, 09:13 AM
The simplest method is just to use a StreamReader and read the data as you see fit. If you look in the System.IO namespace you should find more than enough options for reading it.

The other question is what kind of analysis do you need to do on it. You'll likely need to use the System.Text and/or System.Text.RegularExpressions namespaces.

Shuja Ali
June 2nd, 2005, 09:16 AM
Hello, I'm a C# beginner and have a simple question. I need to read an ascii file which is roughly 640kb - 2Mb and then analyse it, i.e. read it line for line. Can anyone recommend any object\methods for doing such a task.

Links to related sites would be nice.

Thanks :) You have to use the StreamReader class that is present in System.IO namespace..

the code will look somewhat like this..

// Create an instance of StreamReader and pass a filename to it
using (StreamReader myFileStream = new StreamReader("myASCIIFILE.TXT"))
{
String sLine;
// Read the file line by line till the end of file is reached
while ((sLine = myFileStream.ReadLine()) != null)
{
//Your code for analysing the Line goes here
}
}


you need to put using System.IO; at the top of your Class/Form..

PS: Take a look at the StreamReader Class in your MSDN Documentation

laasunde
June 3rd, 2005, 01:29 AM
Thanks to you both.

laasunde
June 3rd, 2005, 01:45 AM
What is the function of using in the below example ?

using (StreamReader myFileStream = new StreamReader("myASCIIFILE.TXT"))

Code worked fine btw :)

Norfy
June 3rd, 2005, 02:42 AM
What is the function of using in the below example ? Have you looked the keyword up but do not understand something about its usage?

laasunde
June 3rd, 2005, 02:57 AM
Have you looked the keyword up but do not understand something about its usage?

I did a search for it but found only reference to c++\namespace which didnt' really match my situation, hence the question.

Norfy
June 3rd, 2005, 03:00 AM
Quick google (http://www.google.com/search?hl=en&q=using+keyword+c%23)

Shuja Ali
June 3rd, 2005, 03:44 AM
What is the function of using in the below example ?

using (StreamReader myFileStream = new StreamReader("myASCIIFILE.TXT"))

Code worked fine btw :)MSDN Ref:
-----------------------------
The using statement defines a scope at the end of which an object will be disposed.

You create an instance in a using statement to ensure that Dispose is called on the object when the using statement is exited. A using statement can be exited either when the end of the using statement is reached or if, for example, an exception is thrown and control leaves the statement block before the end of the statement.

The object you instantiate must implement the System.IDisposable interface.
-----------------------------
PS: In the above example it is used to close the Stream once the operation is finished.. so you don't have to manually dispose the object, it will be automatically disposed when the operation is completed..