Off by one error... avoided!
-
public ArrayList getPathFile(string configFile) { ArrayList sa = new ArrayList(); System.IO.StreamReader file = new System.IO.StreamReader(configFile); while(!file.EndOfStream) { sa.Add(file.ReadLine()); } file.Close(); sa.Remove(""); sa.Remove(""); sa.Remove(""); sa.Remove(""); sa.Remove(""); return sa; }
Wonder what will happen if the configFile ever contains more than 5 empty lines? O_O
-
@hans_mueller said in Off by one error... avoided!:
Wonder what will happen if the configFile ever contains more than 5 empty lines?
My guess? Format exception.
-
public List<string> getPathFile(string configFile) { var sa = new List<string>(); using (var file = new StreamReader(configFile)) while(!file.EndOfStream) sa.Add(file.ReadLine()); return sa.SkipWhile(s => string.IsNullOrWhitespace(s)).ToList(); }
That's much better :D
-
@raceprouk Or this:
public IEnumerable<string> getPathFile(string configFile) { var sa = new List<string>(); using (var file = new StreamReader(configFile)) while(!file.EndOfStream) sa.Add(file.ReadLine()); return sa.SkipWhile(s => string.IsNullOrWhitespace(s)); }
-
@raceprouk Really? Nested lack of braces? Ewwww!
Also, you can simplify it right down to
public IEnumerable<string> getPathFile(string configFile) { return File.ReadAllLines(configFile).Where(l => !string.IsNullOrWhiteSpace(l)); }
-
@jaloopa said in Off by one error... avoided!:
Really? Nested lack of braces? Ewwww!
The weird thing is, I do that in C#, but not in JavaScript
-
@jaloopa said in Off by one error... avoided!:
@raceprouk Really? Nested lack of braces? Ewwww!
Also, you can simplify it right down to
public IEnumerable<string> getPathFile(string configFile) { return File.ReadAllLines(configFile).Where(l => !string.IsNullOrWhiteSpace(l)); }
NOPE, that eliminates blank lines in the middle or at the end....
-
@thecpuwizard The original code will remove the first 5 empty lines, if there are that many, if I'm reading the documentation correctly:
This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
So either could remove empty lines in the middle or the end. Without knowing the actual intent of the code, it's hard to say if it's wrong
-
@jaloopa said in Off by one error... avoided!:
@raceprouk Really? Nested lack of braces? Ewwww!
Also, you can simplify it right down to
public IEnumerable<string> getPathFile(string configFile) { return File.ReadAllLines(configFile).SkipWhile(l => string.IsNullOrWhiteSpace(l)); }
FTFY
-
@raceprouk is there any difference between
SkipWhile(condition)
andWhere(!condition)
?
-
@jaloopa Yes.
Given the collection
1 1 2 2 1 2 1 1 2 1 2
:
SkipWhile(i => i == 1)
will yield2 2 1 2 1 1 2 1 2
Where(i => i != 1)
will yield2 2 2 2 2
-
@raceprouk So we're back to not knowing the original intention of the code. Mine is closer to what the posted version does
-
@jaloopa said in Off by one error... avoided!:
@thecpuwizard The original code will remove the first 5 empty lines, if there are that many, if I'm reading the documentation correctly:
This method performs a linear search; therefore, this method is an O(n) operation, where n is Count.
So either could remove empty lines in the middle or the end. Without knowing the actual intent of the code, it's hard to say if it's wrong
Compared to the original, that is correct [those are differences in their post]. Compared to the previous alternatives (a while loop or skip while) is what I was referring to.
-
Is this the 'C# with Java conventions' thread?
-
@hans_mueller said in Off by one error... avoided!:
new System.IO.StreamReader(configFile)
I really hope you only support one OS / platform and one config file encoding.
This code uses some platform-specific encoding, and you might never know wich one it was after the fact...
-
public IEnumerable<string> getPathFile(string configFile) { return File.ReadAllLines(configFile).SkipWhile(string.IsNullOrWhiteSpace); }
-
@jbert said in Off by one error... avoided!:
This code uses some platform-specific encoding, and you might never know wich one it was after the fact...
As long as it is the right one, does it matter?
-
@dkf
On Windows it is rarely UTF-8 which means it uses whatever Windows codepage you have active or whatever your JVM picks (there could be differences between Oracle and e.g. OpenJDK).The workaround then becomes to launch your JVM with e.g.
-Dfile.encoding=UTF-8
but forgetting it would break your program.