Detecting the end of a line when reading a text file

Z

Z.K.

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

Z.K.

Z.K. said:
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.
 
Z

Z.K.

Z.K. said:
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.
 
D

Dan Manges

Z.K. said:
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
 
Z

Z.K.

Dan said:
Z.K. said:
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.
 
J

Jon Skeet [C# MVP]

Z.K. said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top