CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: read ascii

  1. #1
    Join Date
    Jan 2003
    Posts
    615

    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

  2. #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.

  3. #3
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: read ascii

    Quote 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
    Code:
    using System.IO;
    at the top of your Class/Form..

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

  4. #4
    Join Date
    Jan 2003
    Posts
    615

    Re: read ascii

    Thanks to you both.

  5. #5
    Join Date
    Jan 2003
    Posts
    615

    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

  6. #6
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    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.

  7. #7
    Join Date
    Jan 2003
    Posts
    615

    Re: read ascii

    Quote 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.

  8. #8
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: read ascii

    Quick google
    Useful? Then click on (Rate This Post) at the top of this post.

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: read ascii

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured