Count the lines and check if the file exists



  • Today, i ran accros this code snippet in one of our applications:

    //Count the amount of lines in the file to process
    int LineCount = CountLines(this.Filename);
    
    //Create an array of the size of the line count
    string[] Line;
    Line = new String[LineCount];
    
    // Check if the file is there
    if(File.Exists(this.Filename))
    { 
       doStuff(LineCount); 
    }
    

    Why would anyone want to count the lines of a file BEFORE checking if the file actually exists? What's the point?!

    Oh, and as a small bonus, we get a nice "why do it in one line if I can write two lines instead?"-initialization of an array.



  • Someone might delete your file right after you have counted the lines but before it tried doing stuff on it.
    Race conditions are mucho fun.



  • But what if someone deletes the file before the lines are counted? :P



  • //Count the amount of lines in the file to process
    if(File.Exists(this.Filename))
    {
    int LineCount = CountLines(this.Filename);
    }

    //Create an array of the size of the line count
    string[] Line;
    Line = new String[LineCount];
    
    // Check if the file is there
    if(File.Exists(this.Filename))
    { 
       doStuff(LineCount); 
    }
    

    Fixed! :trollface:



  • Sorry, it won't compile this way: "The name "LineCount" does not exist in the current context"... 😛



  • What if someone stops your process after you count the lines but before you dostuff.



  • Honestly, if your file is guaranteed to be small enough, just call ReadAllLines

    If not, then just lock the file to read before you start. The OS should keep you from deleting the file.



  • @Hans_Mueller said:

    Why would anyone want to count the lines of a file BEFORE checking if the file actually exists? What's the point?!

    As we've discussed before on this forum, unless you're using a co-operatively multitasking OS like Mac Classic or Windows 3.0 there's never a point to checking if a file exists before doing some operation on it. Since the file can be removed between your check and the operation.



  • @blakeyrat said:

    Since the file can be removed between your check and the operation.

    Or even during. Big files, and all that. So error returns or exceptions all over the place it is.

    Anyway, counting lines before reading is something that sounds like old-school memory saving: don't you waste that byte young man! When I was your age, we had to use our bits at least twelve times before we could allocate another bit!



  • @Hans_Mueller said:

    Sorry, it won't compile this way: "The name "LineCount" does not exist in the current context"... 😛

    That was half the point of my :trollface:, actually.



  • @blakeyrat Like all "early" checks, there's no point stability-wise, but there's one comfort-wise, especially when the file not existing is not considered an exceptional condition.

    Especially useful if you're using the debugger and don't want to fiddle with the debugger's exception settings to get the one exception that interests you and not the meaningless FileNotFoundExceptions that slow you down despite the file not being supposed to be there... Even more fun if the exception you're looking for is also a FileNotFoundException.

    PS: I'm looking at you, XML serializer.


Log in to reply