As a quick side-note, this is a perfect time for C# extension methods:

instead of
private static string ReadToDelimiter(StreamReader streamReader, char
delimiter) {}
you could do
public static string ReadToDelimiter(this StreamReader streamReader,
char delimiter) {}
then call it as:
reader.ReadToDelimiter('@');
instead of
ReadToDelimiter(reader, '@');

On Dec 9, 9:40 am, Joe Enos <[EMAIL PROTECTED]> wrote:
> Using String.Split will work fine as long as your line isn't that
> long.  If you've got a huge textfile with a single line, but dozens or
> hundreds of megabytes worth of data, using that method will get you in
> trouble.
>
> I'd go with something like the following - clean it up and modify it
> as appropriate for your needs, including necessary error handling,
> etc...
>
> private static string ReadToDelimiter(StreamReader streamReader, char
> delimiter)
> {
>   StringBuilder result = null;
>   char c = (char)streamReader.Read();
>   while ((!streamReader.EndOfStream) && (c != delimiter))
>   {
>     if (result == null)
>     {
>       result = new StringBuilder();
>     }
>     result.Append(c);
>     c = (char)streamReader.Read();
>   }
>   return (result != null) ? result.ToString() : null;
>
> }
>
> To call your code, you can do something like:
> StreamReader reader = new StreamReader(@"C:\myfile.txt");
> string textLine = ReadToDelimiter(reader, '@');
> while (textLine != null)
> {
>   Console.WriteLine(textLine);
>   textLine = ReadToDelimiter(reader, '@');
>
> }
>
> On Dec 9, 8:24 am, "Kaarthik Padmanabhan" <[EMAIL PROTECTED]>
> wrote:
>
> > aah..forgot to replace the path param passed in:
> > Initialize the StreamReader as follows:
> > sr = New System.IO.StreamReader(path) instead of the hard-coded dummy path
> > used.
>
> > On Tue, Dec 9, 2008 at 8:51 PM, Kaarthik Padmanabhan
> > <[EMAIL PROTECTED]>wrote:
>
> > > Hi,
> > > See if this helps:
>
> > >     Private Function GetDelimitedString(ByVal path As String) As List(Of
> > > String)
> > >         Dim sr As System.IO.StreamReader
> > >         Dim contentStr As String = String.Empty
> > >         Dim delimitedStringList As New List(Of String)
>
> > >         sr = New System.IO.StreamReader("C:\DelimitedTextFile.txt")
> > >         sr.ReadLine()
> > >         contentStr = sr.ReadToEnd()
>
> > >         If Not String.IsNullOrEmpty(contentStr) Then
> > >             For Each delimitedStr As String In contentStr.Split("@")
> > >                 delimitedStringList.Add(delimitedStr)
> > >             Next
> > >         End If
>
> > >         Return delimitedStringList
> > >     End Function
>
> > > Regards,
> > > Kaarthik
>
> > > On Tue, Dec 9, 2008 at 3:27 PM, Clyde <[EMAIL PROTECTED]> wrote:
>
> > >> Hello,
> > >>  I have a txt file whose lines are separated by a delimiter "@".
> > >> How can I read the file line by line using "streamreader"?
> > >> Plz reply asap.
> > >> Thanx,
> > >> Clyde
>
> > > --
> > > Kaarthik,
> > >http://coding-passion.blogspot.com
>
> > --
> > Kaarthik,http://coding-passion.blogspot.com
>
>

Reply via email to