Home > Community > Forums > PCB SKILL > How to Read a text file as Text Type

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 How to Read a text file as Text Type 

Last post Mon, Nov 12 2012 12:30 PM by Geoff. 5 replies.
Started by Geoff 12 Nov 2012 11:19 AM. Topic has 5 replies and 1232 views
Page 1 of 1 (6 items)
Sort Posts:
  • Mon, Nov 12 2012 11:19 AM

    • Geoff
    • Top 500 Contributor
    • Joined on Tue, Aug 12 2008
    • Ottawa, Ontario
    • Posts 24
    • Points 390
    How to Read a text file as Text Type Reply

    I am having trouble reading a text file and having it stay as text. I guess skill expects every text item to be quoted.
    Is there a way I can read it as text?

    second choice: can I split each line into an Array?

    sample_infile.il

    775 640 N74011018 TP30CIR_TOP Top
    5045 570 N74011128 TP30CIR_TOP Top
    5175 1015 GND TP30CIR_TOP Top
    55 1310 GND_EARTH TP30CIR_TOP Top
    650 140 VOUT TP30CIR_TOP Top
    500 140 MODEM_48VIN- TP30CIR_TOP Top
    500 190 MODEM_48VIN+ TP30CIR_TOP Top
    75 595 48VIN- TP30CIR_TOP Top
    775 1190 48VIN+ TP30CIR_TOP Top

    (defun Read_String ()
     FileData=infile("sample_infile.txt")
     LocLine=""

     while( LocLine!=nil
     
      LocLine=lineread(FileData)

      printf("%n    %n    %L\n" nth( 0 LocLine) nth( 1 LocLine) nth( 2 LocLine))

     );while

    );defun


    Skill > Read_String
    775   640   N74011018
    5045   570   N74011128
    5175   1015   GND
    55   1310   GND_EARTH
    650   140   VOUT
    500   140   (MODEM_48VIN - TP30CIR_TOP)
    500   190   (MODEM_48VIN + TP30CIR_TOP)
    E-
    E- 75 595 48VIN- TP30CIR_TOP Top
    E-             ^
    E- SYNTAX ERROR found at line 8 column 13 of file sample_infile.txt
    E- *Error* lineread/read: syntax error encountered in input
    ERROR
    Skill >

    Things of note:

    1. The minus and plus sign in MODEM_48VIN-  and  MODEM_48VIN+ gets separated in the output, and that makes a list of two items: (MODEM_48VIN - TP30CIR_TOP)
    2. Program chokes on 48VIN-  maybe because it starts with a number and ends with a minus sign.

    • Post Points: 20
  • Mon, Nov 12 2012 11:48 AM

    • eDave
    • Top 10 Contributor
    • Joined on Sun, Jul 13 2008
    • Christchurch, 00-NZ
    • Posts 736
    • Points 15,930
    Re: How to Read a text file as Text Type Reply

    Use gets instead of lineread:

    (defun Read_String ()

     FileData = infile("sample_infile.txt")

     while(gets(str, FileData)

      LocLine = parseString(str)

      printf("%s    %s    %s\n" nth( 0 LocLine) nth( 1 LocLine) nth( 2 LocLine))

     );while

    );defun 

    Dave Elder, Tait Communications
    • Post Points: 5
  • Mon, Nov 12 2012 11:52 AM

    • eDave
    • Top 10 Contributor
    • Joined on Sun, Jul 13 2008
    • Christchurch, 00-NZ
    • Posts 736
    • Points 15,930
    Re: How to Read a text file as Text Type Reply

    Oops. Use this:

    (defun Read_String ()

     FileData = infile("sample_infile.txt")

     while(gets(str, FileData)

      LocLine = parseString(str, " ")

      printf("%s    %s    %s\n" nth( 0 LocLine) nth( 1 LocLine) nth( 2 LocLine))

     );while

     close(FileData)

    );defun 

    Dave Elder, Tait Communications
    • Post Points: 20
  • Mon, Nov 12 2012 12:13 PM

    • Geoff
    • Top 500 Contributor
    • Joined on Tue, Aug 12 2008
    • Ottawa, Ontario
    • Posts 24
    • Points 390
    Re: How to Read a text file as Text Type Reply

    That worked good except it seems to not exit gracefully from the While loop:

    Skill > Read_String
    775   640   N74011018
    5045   570   N74011128
    5175   1015   GND
    55   1310   GND_EARTH
    650   140   VOUT
    500   140   MODEM_48VIN-
    500   190   MODEM_48VIN+
    75   595   48VIN-
    775   1190   48VIN+
    E- *Error* fprintf/sprintf: format spec. incompatible with data - "Format is '%s   %s   %s\n', argument #2 is nil"
    ERROR
    Skill >

    • Post Points: 20
  • Mon, Nov 12 2012 12:26 PM

    • eDave
    • Top 10 Contributor
    • Joined on Sun, Jul 13 2008
    • Christchurch, 00-NZ
    • Posts 736
    • Points 15,930
    Re: How to Read a text file as Text Type Reply
    You may want to add some checking. ie: when(length(LocLine) >= 3 printf("%s %s %s\n" nth( 0 LocLine) nth( 1 LocLine) nth( 2 LocLine)) )
    Dave Elder, Tait Communications
    • Post Points: 20
  • Mon, Nov 12 2012 12:30 PM

    • Geoff
    • Top 500 Contributor
    • Joined on Tue, Aug 12 2008
    • Ottawa, Ontario
    • Posts 24
    • Points 390
    Re: How to Read a text file as Text Type Reply

    I found the problem. The  text file had some empty lines at the end. Yes, I have to put a check for that.

     Thanks for Your help Dave!

     

    • Post Points: 5
Page 1 of 1 (6 items)
Sort Posts:
Started by Geoff at 12 Nov 2012 11:19 AM. Topic has 5 replies.