Loading ...
Sorry, an error occurred while loading the content.

Re: Problems opening a txt file for reading

Expand Messages
  • Damien Carbery
    ... The backslashes are not being interpreted as you want them. I know of 3 choices: use forward slashes (yes, it works on Windows) or use double backslashes
    Message 1 of 12 , Oct 5, 2006
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "chipwillis2003"
      <chipwillis2003@...> wrote:
      >
      > Hi,
      > I have been tasked to convert the code I wrote in ASP to Perl. I
      > have never written anything in Perl. The program I am working with
      > is iLinc. It has been re-written in Perl. I have installed the new
      > version of ilinc and have written a simple hello world program using
      > a variable and it works. The people at ilinc said that by installing
      > their program it also installs Perl and I should not have a problem
      > running Perl code. The OS is Windows 2003 Server. I am a system
      > administrator but have done a lot of web programming. This code
      > should work in a browser (IE)?
      >
      > #!/usr/bin/perl -wT
      >
      > $name = 'chip';
      > print "Hello, $name!\n";
      >
      >
      > Next I try to open a text file but this does not work.
      >
      > #!/usr/bin/perl -wT
      >
      > open (TXTFILE, "c:\Program Files\iLinc\Server\Web\Test\CLASS.TXT");
      > @lines = <TXTFILE>;
      > close(TXTFILE);
      >
      > All the file extensions are .pl
      >
      > The text file is there and does not have any attributes set.
      > What am I not doing?
      >
      > Thank you,
      > Chip Willis
      >

      The backslashes are not being interpreted as you want them.
      I know of 3 choices: use forward slashes (yes, it works on Windows) or
      use double backslashes or use single quotes:

      # Forward slashes work in Windows (and other platforms - it's
      # the protable solution.
      open (TXTFILE, "c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT");
      # Double up on the backslashes so "\\" is interpreted as \.
      open (TXTFILE, "c:\\Program Files\\iLinc\\Server\\Web\\Test\\CLASS.TXT");
      # perl won't try to change the text inside single quotes.
      open (TXTFILE, 'c:\Program Files\iLinc\Server\Web\Test\CLASS.TXT');
    • chip willis
      I have tried all the code below and none has worked. I still get a page can not be displayed error. The file is in the same folder as the web page
      Message 2 of 12 , Oct 5, 2006
      • 0 Attachment
        I have tried all the code below and none has worked. I still get a "page can not be displayed" error. The file is in the same folder as the web page "openfile.pl" but even without the path it gives me the same error. I tried Doubling up on the backslashes and single quotes and forward slashes.
        Any other ideas?

        Damien Carbery <daymobrew@...> wrote:
        --- In perl-beginner@yahoogroups.com, "chipwillis2003"
        <chipwillis2003@...> wrote:
        >
        > Hi,
        > I have been tasked to convert the code I wrote in ASP to Perl. I
        > have never written anything in Perl. The program I am working with
        > is iLinc. It has been re-written in Perl. I have installed the new
        > version of ilinc and have written a simple hello world program using
        > a variable and it works. The people at ilinc said that by installing
        > their program it also installs Perl and I should not have a problem
        > running Perl code. The OS is Windows 2003 Server. I am a system
        > administrator but have done a lot of web programming. This code
        > should work in a browser (IE)?
        >
        > #!/usr/bin/perl -wT
        >
        > $name = 'chip';
        > print "Hello, $name!\n";
        >
        >
        > Next I try to open a text file but this does not work.
        >
        > #!/usr/bin/perl -wT
        >
        > open (TXTFILE, "c:\Program Files\iLinc\Server\Web\Test\CLASS.TXT");
        > @lines = <TXTFILE>;
        > close(TXTFILE);
        >
        > All the file extensions are .pl
        >
        > The text file is there and does not have any attributes set.
        > What am I not doing?
        >
        > Thank you,
        > Chip Willis
        >

        The backslashes are not being interpreted as you want them.
        I know of 3 choices: use forward slashes (yes, it works on Windows) or
        use double backslashes or use single quotes:

        # Forward slashes work in Windows (and other platforms - it's
        # the protable solution.
        open (TXTFILE, "c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT");
        # Double up on the backslashes so "\\" is interpreted as \.
        open (TXTFILE, "c:\\Program Files\\iLinc\\Server\\Web\\Test\\CLASS.TXT");
        # perl won't try to change the text inside single quotes.
        open (TXTFILE, 'c:\Program Files\iLinc\Server\Web\Test\CLASS.TXT');






        ---------------------------------
        Do you Yahoo!?
        Get on board. You're invited to try the new Yahoo! Mail.

        [Non-text portions of this message have been removed]
      • Mike Southern
        ... You need to escape the directory slashes in the path: open (TXTFILE, c: Program Files iLinc Server Web Test CLASS.TXT ); But after that you don t
        Message 3 of 12 , Oct 5, 2006
        • 0 Attachment
          On 10/5/06 12:14 PM, chipwillis2003 at chipwillis2003@... wrote:

          > #!/usr/bin/perl -wT
          >
          > $name = 'chip';
          > print "Hello, $name!\n";
          >
          >
          > Next I try to open a text file but this does not work.
          >
          > #!/usr/bin/perl -wT
          >
          > open (TXTFILE, "c:\Program Files\iLinc\Server\Web\Test\CLASS.TXT");
          > @lines = <TXTFILE>;
          > close(TXTFILE);
          >
          > All the file extensions are .pl
          >
          > The text file is there and does not have any attributes set.
          > What am I not doing?

          You need to escape the directory slashes in the path:

          open (TXTFILE, "c:\\Program Files\\iLinc\\Server\\Web\\Test\\CLASS.TXT");

          But after that you don't say how you know that the file doesn't open.

          You've assigned the content to @lines, but what do you do next? I.E. What is
          the next bit of code that you haven't supplied here ...
        • merlyn@stonehenge.com
          ... Mike You need to escape the directory slashes in the path: Mike open (TXTFILE, c: Program Files iLinc Server Web Test CLASS.TXT ); or just use
          Message 4 of 12 , Oct 5, 2006
          • 0 Attachment
            >>>>> "Mike" == Mike Southern <gb1198@...> writes:

            Mike> You need to escape the directory slashes in the path:

            Mike> open (TXTFILE, "c:\\Program Files\\iLinc\\Server\\Web\\Test\\CLASS.TXT");

            or just use forward slashes:

            c:/Program Files/iLinc ...

            I don't know why everyone is so addicted to the backslashes. :)

            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
            See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
          • Mike Southern
            ... Habit. Like learning to drive a truck is largely eliminating bad car driving habits, so learning to write real software is elminiating all the bad things
            Message 5 of 12 , Oct 5, 2006
            • 0 Attachment
              On 10/5/06 3:48 PM, Randal L. Schwartz at merlyn@... wrote:

              > or just use forward slashes:
              >
              > c:/Program Files/iLinc ...
              >
              > I don't know why everyone is so addicted to the backslashes. :)

              Habit.

              Like learning to drive a truck is largely eliminating bad car driving
              habits, so learning to write real software is elminiating all the bad things
              we used to do in DOS ...
            • chip willis
              Forward slashes are fine with me. I do nothing at the moment with the txt file. I will after opening it split it into an array. The only thing in the txt file
              Message 6 of 12 , Oct 5, 2006
              • 0 Attachment
                Forward slashes are fine with me.
                I do nothing at the moment with the txt file. I will after opening it split it into an array. The only thing in the txt file at the moment is (without quotes) "this is a test".

                What needs to be done is to take the txt file which is a CSV file (comma delimited file), read it into an array and then take what I want out of the array and send it in XML to some API functions that ilinc (the software vender) has written which will create classes and register students into the classes and the instructor. Also create instructors and change network settings and so forth. I do this now with the ASP code but did not need to use the XML. I am just trying to do one thing (no so simple for me obviously) at a time. The CVS file is an exported file from PeopleSoft (software which the classes are originally input in here at the university) and import them into the ilinc software. iLinc is all web based distance learning software and has other prices for staff etc.

                So even if I try this:

                #!/usr/bin/perl -wT

                open (TXTFILE, "c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT");
                #@lines = <TXTFILE>;
                #close(TXTFILE);

                I get the error on the web page that “The page can not be displayed” which means there is some error while trying to open the file.
                Is there a good Perl editor to use that will give me better error handling messages? I have tried DzSoft Perl Editor but it does not seem to work well with web pages or I have not figured it out yet.
                Thanks for your help,
                Chip



                Mike Southern <gb1198@...> wrote:
                On 10/5/06 12:14 PM, chipwillis2003 at chipwillis2003@... wrote:

                > #!/usr/bin/perl -wT
                >
                > $name = 'chip';
                > print "Hello, $name!\n";
                >
                >
                > Next I try to open a text file but this does not work.
                >
                > #!/usr/bin/perl -wT
                >
                > open (TXTFILE, "c:\Program Files\iLinc\Server\Web\Test\CLASS.TXT");
                > @lines = <TXTFILE>;
                > close(TXTFILE);
                >
                > All the file extensions are .pl
                >
                > The text file is there and does not have any attributes set.
                > What am I not doing?

                You need to escape the directory slashes in the path:

                open (TXTFILE, "c:\\Program Files\\iLinc\\Server\\Web\\Test\\CLASS.TXT");

                But after that you don't say how you know that the file doesn't open.

                You've assigned the content to @lines, but what do you do next? I.E. What is
                the next bit of code that you haven't supplied here ...






                ---------------------------------
                Get your own web address for just $1.99/1st yr. We'll help. Yahoo! Small Business.

                [Non-text portions of this message have been removed]
              • Mike Southern
                ... But you aren t performing any function on the contents of the file yet. You have just opened it and assigned it to an array. You then need to work on the
                Message 7 of 12 , Oct 5, 2006
                • 0 Attachment
                  On 10/5/06 5:31 PM, chip willis at chipwillis2003@... wrote:

                  > open (TXTFILE, "c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT");
                  > #@lines = <TXTFILE>;
                  > #close(TXTFILE);
                  >
                  > I get the error on the web page that ³The page can not be displayed² which
                  > means there is some error while trying to open the file.

                  But you aren't performing any function on the contents of the file yet. You
                  have just opened it and assigned it to an array.

                  You then need to work on the contents of that array and present them to your
                  end product - in this case a web page.

                  so:

                  Open file
                  Assign file contents to array
                  Close file

                  For each record (line) of the array {
                  print the line
                  } #end the loop

                  For example:

                  foreach $line(@lines) {
                  print "$line\n";
                  }

                  In reality you would use CGI to format that print line for HTML but the end
                  result for each line could be

                  print "<p>$line</p>";
                • ashish_gupta_cal
                  Also try checking if file got successfully opened. It might help. open (TXTFILE, c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT ) or die Cant open file :
                  Message 8 of 12 , Oct 5, 2006
                  • 0 Attachment
                    Also try checking if file got successfully opened. It might help.

                    open (TXTFILE, "c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT") or
                    die "Cant open file : $!";

                    Regards,
                    Ashish

                    --- In perl-beginner@yahoogroups.com, Mike Southern <gb1198@...> wrote:
                    >
                    > On 10/5/06 5:31 PM, chip willis at chipwillis2003@... wrote:
                    >
                    > > open (TXTFILE, "c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT");
                    > > #@lines = <TXTFILE>;
                    > > #close(TXTFILE);
                    > >
                    > > I get the error on the web page that ³The page can not be
                    displayed² which
                    > > means there is some error while trying to open the file.
                    >
                    > But you aren't performing any function on the contents of the file
                    yet. You
                    > have just opened it and assigned it to an array.
                    >
                    > You then need to work on the contents of that array and present them
                    to your
                    > end product - in this case a web page.
                    >
                    > so:
                    >
                    > Open file
                    > Assign file contents to array
                    > Close file
                    >
                    > For each record (line) of the array {
                    > print the line
                    > } #end the loop
                    >
                    > For example:
                    >
                    > foreach $line(@lines) {
                    > print "$line\n";
                    > }
                    >
                    > In reality you would use CGI to format that print line for HTML but
                    the end
                    > result for each line could be
                    >
                    > print "<p>$line</p>";
                    >
                  • chip willis
                    Yes, adding this line works great: foreach $line(@lines) { print $line n ; } Seems without doing anything with the file caused the error. Thanks, Chip Mike
                    Message 9 of 12 , Oct 5, 2006
                    • 0 Attachment
                      Yes, adding this line works great:

                      foreach $line(@lines) {
                      print "$line\n";
                      }

                      Seems without doing anything with the file caused the error.
                      Thanks,
                      Chip

                      Mike Southern <gb1198@...> wrote:
                      On 10/5/06 5:31 PM, chip willis at chipwillis2003@... wrote:

                      > open (TXTFILE, "c:/Program Files/iLinc/Server/Web/Test/CLASS.TXT");
                      > #@lines = <TXTFILE>;
                      > #close(TXTFILE);
                      >
                      > I get the error on the web page that ³The page can not be displayed² which
                      > means there is some error while trying to open the file.

                      But you aren't performing any function on the contents of the file yet. You
                      have just opened it and assigned it to an array.

                      You then need to work on the contents of that array and present them to your
                      end product - in this case a web page.

                      so:

                      Open file
                      Assign file contents to array
                      Close file

                      For each record (line) of the array {
                      print the line
                      } #end the loop

                      For example:

                      foreach $line(@lines) {
                      print "$line\n";
                      }

                      In reality you would use CGI to format that print line for HTML but the end
                      result for each line could be

                      print "<p>$line</p>";






                      ---------------------------------
                      Stay in the know. Pulse on the new Yahoo.com. Check it out.

                      [Non-text portions of this message have been removed]
                    • Mike Southern
                      ... I don t think there was really an error - you were assuming that just by opening the file a process would automatically do something with the content.
                      Message 10 of 12 , Oct 5, 2006
                      • 0 Attachment
                        On 10/5/06 6:49 PM, chip willis at chipwillis2003@... wrote:

                        > Yes, adding this line works great:
                        >
                        > foreach $line(@lines) {
                        > print "$line\n";
                        > }
                        >
                        > Seems without doing anything with the file caused the error.
                        > Thanks,
                        > Chip

                        I don't think there was really an error - you were assuming that just by
                        opening the file a "process" would automatically do something with the
                        content.

                        And in a more detailed reality, the above "print" example may not work the
                        way you expect when you serve it to a HTML browser.

                        Each data component that you wish to display has to be formatted using HTML
                        as if you were writing the HTML content by hand, including the required
                        document tags.

                        Thus your work may start to look like this:

                        print "<html>";
                        print "<head>";
                        print "<title>Page title</title>";
                        print "</head>";
                        print "<body>";


                        foreach $line(@lines) {
                        print "<p>$line</p>";
                        }

                        print "</body>";
                        print "</html>";

                        But DON'T do that ... read about, and use, CGI.pm instead as it will save
                        you a ton of time spent formatting HTML.
                      • chip willis
                        Thanks for the info. What I will be doing is putting the contents of the txt file, a comma delimited file into a Windows SQL database using the venders suplied
                        Message 11 of 12 , Oct 8, 2006
                        • 0 Attachment
                          Thanks for the info.
                          What I will be doing is putting the contents of the txt file, a comma delimited file into a Windows SQL database using the venders suplied API. So there will not need to be any HTML formating. I will no doubt need to learn that also with GGI.pm
                          Thanks
                          Chip

                          Mike Southern <gb1198@...> wrote:
                          On 10/5/06 6:49 PM, chip willis at chipwillis2003@... wrote:

                          > Yes, adding this line works great:
                          >
                          > foreach $line(@lines) {
                          > print "$line\n";
                          > }
                          >
                          > Seems without doing anything with the file caused the error.
                          > Thanks,
                          > Chip

                          I don't think there was really an error - you were assuming that just by
                          opening the file a "process" would automatically do something with the
                          content.

                          And in a more detailed reality, the above "print" example may not work the
                          way you expect when you serve it to a HTML browser.

                          Each data component that you wish to display has to be formatted using HTML
                          as if you were writing the HTML content by hand, including the required
                          document tags.

                          Thus your work may start to look like this:

                          print "<html>";
                          print "<head>";
                          print "<title>Page title</title>";
                          print "</head>";
                          print "<body>";

                          foreach $line(@lines) {
                          print "<p>$line</p>";
                          }

                          print "</body>";
                          print "</html>";

                          But DON'T do that ... read about, and use, CGI.pm instead as it will save
                          you a ton of time spent formatting HTML.






                          ---------------------------------
                          Do you Yahoo!?
                          Everyone is raving about the all-new Yahoo! Mail.

                          [Non-text portions of this message have been removed]
                        Your message has been successfully submitted and would be delivered to recipients shortly.