You'll have to put in a conditional check after
line = reader2.ReadLine();
to check of EndOfStream and if this is true, your conditional check for the line (either a RegEx or line.Length == 5).
You'll have to put in a conditional check after
line = reader2.ReadLine();
to check of EndOfStream and if this is true, your conditional check for the line (either a RegEx or line.Length == 5).
Create a RegEx that matches a 4 or 5 digit string and filter your line removal on that. There are tons of good site online for C# RegEx practice/learning.
Add that after the end of the while
loop :
if (!Regex.IsMatch(line, @"^\d{4,5}$"))
writer2.WriteLine(line);
To keep the same logic as you have in the current code you could do the following
//Delete the last line from the file. This line could be 8174, 10000, or anything. This is from SO
string tempfile = @"C:\junk_temp.txt";
using (StreamReader reader2 = new StreamReader(newfilename))
{
using (StreamWriter writer2 = new StreamWriter(tempfile))
{
string line = reader2.ReadLine();
while (!reader2.EndOfStream)
{
writer2.WriteLine(line);
line = reader2.ReadLine();
} // by reading ahead, will not write last line to file
// If the last line read does not match your condition
// we write it to the new file
if (!Regex.IsMatch(line, @"^\d{4,5}$"))
{
writer2.WriteLine(line);
}
}
}
File.Delete(newfilename);
File.Move(tempfile, newfilename);
File.Delete(tempfile);
Change your while
to:
while (!reader2.EndOfStream)
{
string line = reader2.ReadLine();
if (reader2.EndOfStream && (CheckIfNeedToDeleteLine(line) == false))
{
writer2.WriteLine(line);
}
}
You will need to implement CheckIfNeedToDeleteLine(line)
.
bool CheckIfNeedToDeleteLine(string line)
{
int digit = 0;
return (((line.Length == 4) || (line.Length == 5)) &&
(int.TryParse(line, out digit)))
}
Regex
implementation of CheckIfNeedToDeleteLine
:
bool CheckIfNeedToDeleteLine(string line)
{
return Regex.IsMatch(line, @"^\d{4,5}$");
}
No idea which is more efficient but it wouldn't really matter since it would only happen on the last iteration because of the order of the if
statement.
I'm reluctant to "modify the code" directly for you, but I'll give you a pointer.
string line = reader2.ReadLine();
while (!reader2.EndOfStream)
{
writer2.WriteLine(line);
line = reader2.ReadLine();
} // by reading ahead, will not write last line to file
Right after the last brace }
in this code, you have the last line of the file in the variable line
. In your current code, this line gets thrown away. I'd suggest that you insert an "if" statement here, comparing this last line to your criteria. You could use a regular expression for this, something like ^[0-9]{4,5}$
should be suitable.
If the criteria is met, just call writer2.WriteLine(line);
one more time for the last line.
string tempfile = @"C:\junk_temp.txt";
using (StreamReader reader2 = new StreamReader(newfilename))
{
using (StreamWriter writer2 = new StreamWriter(tempfile))
{
string line = reader2.ReadLine();
while (!reader2.EndOfStream)
{
writer2.WriteLine(line);
line = reader2.ReadLine();
} // by reading ahead, will not write last line to file
int result=0;
if (Int32.TryParse(s, out result) )
{
if (s.Length != 4 || s.Length != 5)
writer2.WriteLine(line);
}
}
}
File.Delete(newfilename);
File.Move(tempfile, newfilename);
File.Delete(tempfile);