By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

Detecting the end of a line when reading a text file

P: n/a
In C#, using the StreamReader, how do I detect when you get to the end
of line. I am reading a text file using the Read() function and I need
to detect the \n\r, but everything I try does not work. I am sure that
this probably fairly simple, but I have not been able to figure it out.

Z.K.
Jun 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Z.K. wrote:
In C#, using the StreamReader, how do I detect when you get to the end
of line. I am reading a text file using the Read() function and I need
to detect the \n\r, but everything I try does not work. I am sure that
this probably fairly simple, but I have not been able to figure it out.

Z.K.


Never mind, I figured it out.
Jun 5 '06 #2

P: n/a
Z.K. wrote:
Z.K. wrote:
In C#, using the StreamReader, how do I detect when you get to the end
of line. I am reading a text file using the Read() function and I
need to detect the \n\r, but everything I try does not work. I am
sure that this probably fairly simple, but I have not been able to
figure it out.

Z.K.

Never mind, I figured it out.


Actually, I still had a problem, but now I have figured it out.
Actually it is simpler than I expected.
char charInput;
int input = 0;

while(freader.Peek() > -1)
{
input = freader.Read();
//charInput = (char)input;
arList.Add((char)Input);

}

freader.Close();
Z.K.
Jun 6 '06 #3

P: n/a
Z.K. wrote:
Never mind, I figured it out.


Actually, I still had a problem, but now I have figured it out. Actually
it is simpler than I expected.
char charInput;
int input = 0;

while(freader.Peek() > -1)
{
input = freader.Read();
//charInput = (char)input;
arList.Add((char)Input);

}

freader.Close();
Z.K.


Z.K.,

You know that your code is detecting the end of the file, not the end of
a line in the file, right? I thought your original question was
detecting the end of lines. If that's the case, then StreamReader has a
ReadLine() method which will help.

Also, processing one character at a time and adding to an ArrayList of
characters will not be very efficient regarding performance (unless the
input is small).

You may want to try something like:

StreamReader reader = new StreamReader(an_input_stream);
int buffer_size = 256;
char[] char_buffer = new char[buffer_size];
StringBuilder result = new StringBuilder();
using (reader)
{
int count = reader.Read(char_buffer, 0, buffer_size);
while (count > 0)
{
result.Append(char_buffer, 0, count);
count = reader.Read(char_buffer, 0, buffer_size);
}
}
return result.ToString();

(Note: I didn't compile this, so the code might not compile directly.)

Hope this helps.

Dan Manges
Jun 6 '06 #4

P: n/a
Dan Manges wrote:
Z.K. wrote:
Never mind, I figured it out.

Actually, I still had a problem, but now I have figured it out.
Actually it is simpler than I expected.
char charInput;
int input = 0;

while(freader.Peek() > -1)
{
input = freader.Read();
//charInput = (char)input;
arList.Add((char)Input);
}
freader.Close();
Z.K.

Z.K.,

You know that your code is detecting the end of the file, not the end of
a line in the file, right? I thought your original question was
detecting the end of lines. If that's the case, then StreamReader has a
ReadLine() method which will help.

Also, processing one character at a time and adding to an ArrayList of
characters will not be very efficient regarding performance (unless the
input is small).

You may want to try something like:

StreamReader reader = new StreamReader(an_input_stream);
int buffer_size = 256;
char[] char_buffer = new char[buffer_size];
StringBuilder result = new StringBuilder();
using (reader)
{
int count = reader.Read(char_buffer, 0, buffer_size);
while (count > 0)
{
result.Append(char_buffer, 0, count);
count = reader.Read(char_buffer, 0, buffer_size);
}
}
return result.ToString();

(Note: I didn't compile this, so the code might not compile directly.)

Hope this helps.

Dan Manges

Ok, this is my problem. I have written the ascii character set to a
file as strings followed by a series of other strings. Now when I read
it back I get extra extra blank lines just after character 10 and 13
which is the \r \n and also just after the end of the ascii character
set and the beginning of my normal strings. I am trying somehow to
detect those characters so that I can format the file properly and read
it back properly. I am having some trouble doing this as I can't find a
good way to detect the ascii character.

Thanks for the code though, it might be useful later.

Z.K.
Jun 7 '06 #5

P: n/a
Z.K. <no****@nospam.net> wrote:
Ok, this is my problem. I have written the ascii character set to a
file as strings followed by a series of other strings. Now when I read
it back I get extra extra blank lines just after character 10 and 13
which is the \r \n and also just after the end of the ascii character
set and the beginning of my normal strings. I am trying somehow to
detect those characters so that I can format the file properly and read
it back properly. I am having some trouble doing this as I can't find a
good way to detect the ascii character.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.