Home > Community > Forums > PCB SKILL > need tips and examples for reading a text file

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: *

 need tips and examples for reading a text file 

Last post Thu, Jan 3 2008 1:13 PM by archive. 4 replies.
Started by archive 03 Jan 2008 01:13 PM. Topic has 4 replies and 2532 views
Page 1 of 1 (5 items)
Sort Posts:
  • Thu, Jan 3 2008 1:13 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    need tips and examples for reading a text file Reply

    I'm writing some axlSkill that reads the variants.lst file.
    The statement xx = infile("variants.lst") opens the file for reading.
    The statement gets(s xx) gets the first line written to a string object s.
    Now I'm trying to use a getchar call to look at each character of the string and then decide what to do with the line. Specifically I'm looking for strings that start with "(" (for example).
    I try getchar(s 1) == "(" hoping for a return value of t. But even though the first character of string s is ( the getchar call returns nil.
    I'm guessing there is an easy way to parse a string and I haven't found it.
    Help?


    Originally posted in cdnusers.org by AshCan
    • Post Points: 0
  • Thu, Jan 3 2008 1:41 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: need tips and examples for reading a text file Reply

    getchar returns a character not a string so do
    getchar(s 1) == '\(

    also since a paren is special in Skill it needs to be escaped by a backslash, to match a 'd' all you would need is a 'd


    Originally posted in cdnusers.org by fxf
    • Post Points: 0
  • Thu, Jan 3 2008 2:11 PM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: need tips and examples for reading a text file Reply

    Some other options:
    1) use substring instead of getchar.
    substring(s 1 1) == "("
    2) convert the getchar result to a string
    get_string(getchar(s 1)) == "("


    Originally posted in cdnusers.org by Randy R.
    • Post Points: 0
  • Fri, Jan 4 2008 9:31 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: need tips and examples for reading a text file Reply

    Thanks! Another - even more general - question.
    I've got a major brain freeze going here.

    I'm using a while to read a text file (the variants.lst file). In this example I look at the first character of each line and  see if it is an open paren.

         xx = infile("variants.lst")
         count = 0
         while(xx
             gets(s xx)
             lineno = count++
             if(substring(s 1 1)=="("
                then
                printf("Line #%d starts with a \"(\"\n" lineno)
                else
                printf("Line #%d doesn't start with a \"(\"\n" lineno)
             ); end if
         ); end while

    Problem is that after the last line of the infile is read I get:
    E- *Error* substring:
    argument #1 should be either a string or a symbol (type template = "Sx") - nil

    The last gets statement has returned a string that is nil.

    If I put in an if loop  if(s != nil  after the gets statement the while loop goes forever. How do I evaluate the result of the gets statement before trying to use it?


    Originally posted in cdnusers.org by AshCan
    • Post Points: 0
  • Fri, Jan 4 2008 11:02 AM

    • archive
    • Top 75 Contributor
    • Joined on Fri, Jul 4 2008
    • Posts 88
    • Points 4,930
    RE: need tips and examples for reading a text file Reply

    See if this works:
    if( xx = infile("variants.lst") ; make sure we can open the file
    then
    count = 0
    while( gets(s xx)
    lineno = count++
    if(substring(s 1 1)=="("
    then
    printf("Line #%d starts with a \"(\"\n" lineno)
    else
    printf("Line #%d doesn't start with a \"(\"\n" lineno)
    ); end if
    ); end while
    close(xx); close out the input port
    ); end if


    Originally posted in cdnusers.org by Randy R.
    • Post Points: 0
Page 1 of 1 (5 items)
Sort Posts:
Started by archive at 03 Jan 2008 01:13 PM. Topic has 4 replies.