|
-
June 2nd, 2005, 09:06 AM
#1
read ascii
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
-
June 2nd, 2005, 09:13 AM
#2
Re: read ascii
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.
-
June 2nd, 2005, 09:16 AM
#3
Re: read ascii
 Originally Posted by laasunde
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..
Code:
// 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 at the top of your Class/Form..
PS: Take a look at the StreamReader Class in your MSDN Documentation
-
June 3rd, 2005, 01:29 AM
#4
-
June 3rd, 2005, 01:45 AM
#5
Re: read ascii
What is the function of using in the below example ?
Code:
using (StreamReader myFileStream = new StreamReader("myASCIIFILE.TXT"))
Code worked fine btw
-
June 3rd, 2005, 02:42 AM
#6
Re: read ascii
What is the function of using in the below example ?
Have you looked the keyword up but do not understand something about its usage?
Useful? Then click on (Rate This Post) at the top of this post.
-
June 3rd, 2005, 02:57 AM
#7
Re: read ascii
 Originally Posted by Norfy
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.
-
June 3rd, 2005, 03:00 AM
#8
Re: read ascii
Useful? Then click on (Rate This Post) at the top of this post.
-
June 3rd, 2005, 03:44 AM
#9
Re: read ascii
 Originally Posted by laasunde
What is the function of using in the below example ?
Code:
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..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|