Problem checking what the last line in a file is.

  • Thread starter Thread starter CCLeasing
  • Start date Start date
C

CCLeasing

Hi i'm trying to check if the last line in a file is equal to a string
i'm comparing it with.

I have the general idea of what I should be doing. Could someone take a
look at the following and comment it or suggest improvements. As code
it doesn't work at the moment but I hope from it you can see what i'm
trying to do.

private bool checkduplicates(string filepath, string linetocheck)
{
string[] lines = File.ReadAllLines(@filepath);
if lines[last element?].ToString() = linetocheck then
return true;
else return false;
}

basically i want to be able to provide two arguments to checkduplicates
function.
filepath and linetocheck.

i want to open the file at filepath, read it into an array line by
line. and then compare the last line of the file with the string
linetocheck. I want to return true if the line matches, and false if it
doesn't.

Thanks.
 
Hi,

I assume you are wondering what to replace with [last element?]. You can
use the Length property of an Array for this.

if(lines[lines.Length - 1] == linetocheck)
return true;
else
return false;

ToString() isn't necessary as lines is an array of String

Hi i'm trying to check if the last line in a file is equal to a string
i'm comparing it with.

I have the general idea of what I should be doing. Could someone take a
look at the following and comment it or suggest improvements. As code
it doesn't work at the moment but I hope from it you can see what i'm
trying to do.

private bool checkduplicates(string filepath, string linetocheck)
{
string[] lines = File.ReadAllLines(@filepath);
if lines[last element?].ToString() = linetocheck then
return true;
else return false;
}

basically i want to be able to provide two arguments to checkduplicates
function.
filepath and linetocheck.

i want to open the file at filepath, read it into an array line by
line. and then compare the last line of the file with the string
linetocheck. I want to return true if the line matches, and false if it
doesn't.

Thanks.
 
CCLeasing said:
Hi i'm trying to check if the last line in a file is equal to a string
i'm comparing it with.

I have the general idea of what I should be doing. Could someone take a
look at the following and comment it or suggest improvements. As code
it doesn't work at the moment but I hope from it you can see what i'm
trying to do.

private bool checkduplicates(string filepath, string linetocheck)
{
string[] lines = File.ReadAllLines(@filepath);
if lines[last element?].ToString() = linetocheck then
return true;
else return false;
}

That's potentially expensive way of doing things in terms of memory.
What would be better would be to read the file one line at a time,
keeping track of the last line you've read. Once you've read all the
lines, *then* you can do the check. At that point, the check would just
be:

return (lineToCheck==lastLine);

Note that depending on the character encoding of the file, it *may* be
reasonably straightforward to read the file "backwards" to look for the
last line, which would save going through the whole lot of it, but that
would be fairly tricky - not worth it unless the file could be pretty
large.

Jon
 
Jon said:
Note that depending on the character encoding of the file, it *may* be
reasonably straightforward to read the file "backwards" to look for the
last line, which would save going through the whole lot of it, but that
would be fairly tricky - not worth it unless the file could be pretty
large.

I just happen to have a little code snippet for that:

public static string ReadLastLine(string fnm)
{
FileStream fs = new FileStream(fnm, FileMode.Open,
FileAccess.Read);
long l = fs.Length;
int n = 1000;
long ix = l - n;
if(ix < 0)
{
n = n + (int)ix;
ix = 0;
}
fs.Seek(ix, SeekOrigin.Begin);
byte[] b = new byte[n];
fs.Read(b, 0, n);
fs.Close();
int nl = n - 2;
while(nl >= 0 && b[nl] != '\n') nl--;
nl++;
return Encoding.Default.GetString(b, nl, n - nl);
}

It makes some assumptions:
- the last line is not longer than 1000 bytes
- the encoding needs to be specified (but that is standard for
reading a file from the top also)
- no character is converted to a byte sequence containing 0x0A
but they are usually not that bad.

Arne
 

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

Back
Top