Announcement

Collapse
No announcement yet.

File "open" problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • File "open" problem

    When doing following:

    var f = new File("NoSuchFile.txt");
    if (!f.open("rt")) debugPrintln("open failed");
    else debugPrintln("open worked");

    EFS always says "open worked", even if file does not exist. I'm using eSignal 8.0. Shouldn't it return false?

  • #2
    Hello almerger,

    Your code example is working as it should. You're creating some confusing logic because your putting the "not" operator in front of the file.open() method. When you call the file.open() method, the file opens first, then the if statement evaluates the condition, which evaluates to false because the f.open("rt") call opened the file.

    Try this logic instead.

    PHP Code:
    var = new File("NoSuchFile.txt");
    if (!
    f.isOpen()) debugPrintln("file is not open");
    else 
    debugPrintln("file is open"); 
    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Thanks, Jason. Your method works so I'll use it.

      However, I'm still perplexed. The eSignal doc says:

      "rt opens a file for reading. If the file exists, the method succeeds and returns true; otherwise, the method fails and returns false. "

      Comment


      • #4
        Hello almerger,

        Using the "not" operator ( ! ) in your if() statement is confusing you. Let's write it a slightly different way so it's easier to understand. This is the equivalent logic to your original code.

        PHP Code:
        var = new File("NoSuchFile.txt");
        if (
        f.open("rt") == falsedebugPrintln("open failed");
        else 
        debugPrintln("open worked"); 
        Calling f.open("rt") inside the if() statement opens the NoSuchFile.txt file that you created on the first line. Therefore, f.open("rt") returns true. It is not returning false, which is what your if() statement is asking. The if() statement evaluates to false because if(true == false) is false. In other words, your if() statement is asking if the file is not open, which it is. Therefore, the else portion of your code executes and prints "open worked" to the output window. Does this help?
        Jason K.
        Project Manager
        eSignal - an Interactive Data company

        EFS KnowledgeBase
        JavaScript for EFS Video Series
        EFS Beginner Tutorial Series
        EFS Glossary
        Custom EFS Development Policy

        New User Orientation

        Comment


        • #5
          I appreciate your taking the time to explain this to me, Jason.

          I agree with your re-write of the code. I don't think it's the "not" operator that's confusing me.

          The doc says that the open method returns false if the file is opened "rt" and the file does not exist. Yet the open method returns true. This doesn't make sense to me, whereas it makes sense to you.

          I'm still clueless, but that's OK. As long as I know how it works, I'm happy. Thanks for trying.

          Comment


          • #6
            Hello almerger,

            I'm always happy to help.

            The doc says that the open method returns false if the file is opened "rt" and the file does not exist. Yet the open method returns true.
            That's correct. It would return false if the file did not exist. In your code, you are creating the file.

            var f = new File("NoSuchFile.txt");

            The file exists, which is why the f.open() method returns true. True meaning the file was successfully opened.
            Jason K.
            Project Manager
            eSignal - an Interactive Data company

            EFS KnowledgeBase
            JavaScript for EFS Video Series
            EFS Beginner Tutorial Series
            EFS Glossary
            Custom EFS Development Policy

            New User Orientation

            Comment


            • #7
              So "var f = new File(...)" actually creates the file? I thought it just created a file object. I guess that explains my confusion, though I'm still not comfortable with it. But hey, if it works, that's the bottom line...

              Comment

              Working...
              X