473,327 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Detecting the end of a line when reading a text file

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
5 8685
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: ajikoe | last post by:
Hello, I use windows notepad editor to write text. For example I write (in d:\myfile.txt): Helo World If I open it with python: FName = open(d:\myfile.txt,'r')
7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
2
by: Eric Lilja | last post by:
Hello, I'm writing a simple program that upon start-up needs to open a text file and read a value on the last line. Then all other accesses to the file will be writes (at the end of it). I'm having...
18
by: Fuzzyman | last post by:
Hello all, I'm trying to detect line endings used in text files. I *might* be decoding the files into unicode first (which may be encoded using multi-byte encodings) - which is why I'm not...
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
7
by: iaesun | last post by:
hello, this post assumes executing a program with file redirection as follows: program.exe < intput.txt where input.txt is a text file where there is not necessarily any whitespace after...
10
blazedaces
by: blazedaces | last post by:
Alright guys, so the title explains exactly my goal. The truth is I'm going to be reading in a lot of data from an xml file. The file is too large and there's too much data to store in arraylists...
16
by: mazwolfe | last post by:
Someone recently asked about reading lines. I had this code written some time ago (part of a BASIC-style interpreter based on H. Shildts in Art of C) to read a file with the lines ended in any...
3
by: xyz | last post by:
Hi, I have a text file around 7GB includes 100 million lines... I want to read the data line by line when I approach my module.. ie., when i read for the first time , my program shuld read only...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.