how to read line by line from a file through a OCEAN script

Discussion in 'Cadence' started by Samiran Dam, Aug 29, 2010.

  1. Samiran Dam

    Samiran Dam Guest

    Dear all,

    I need to read from a file in A OCEAN script. The situation is - the
    file stores the data as following:

    1 2 3 4
    5 6 7 8
    9 10 11 12
    ....

    That is in each iteration I have to pick up one line from the file and
    run simulation. Each of the number in a line describes the value of a
    desVar.

    How I can do this....I am not able to do this using gets &
    fscanf....Please help.

    regards,
    Samiran
     
    Samiran Dam, Aug 29, 2010
    #1
    1. Advertisements

  2. Samiran Dam

    Marcel Preda Guest

    Hi there,

    What's the problem with "gets" ?
    I've just try a small example:
    ~~~~~~~~~~~~~~~~~~~~~
    /*
    Read a file with content like:
    1 2 3 4
    5 6 7 8
    9 10 11 12

    and return a list of lists, like
    list( list(1 2 3 4)
    list(5 6 7 8)
    list(9 10 11 12)
    )

    */
    procedure( My_readDataFile(fileName)
    let( (pIn str retList)
    pIn = infile(fileName)
    while(gets(str pIn)
    retList = cons(parseString(str) retList)
    )
    close(pIn)
    reverse(retList) ;; we have retList in reverse order
    )
    )

    My_x = My_readDataFile("/home/predamar/test.in")
    ~~~~~~~~~~~~~~~~~~~~~~

    And it works just perfect

    BR,
    Marcel
     
    Marcel Preda, Aug 30, 2010
    #2
    1. Advertisements

  3. Samiran Dam

    Samiran Dam Guest

    Hi,

    Thanks for the reply.

    I have tried the code you provided as follows:

    "test.ocn"
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    fout=outfile("/home/samiran/Desktop/output.txt")

    let( (pIn str retList)
    pIn = infile("/home/samiran/Desktop/input.txt")
    while(gets(str pIn)
    retList = cons(parseString(str) retList)
    )
    close(pIn)
    reverse(retList) ;; we have retList in reverse order

    foreach(list1 retList
    foreach(num list1
    fprintf(fout "%g " num)
    )
    )

    )

    newline(fout)
    close(fout)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    *Error* fprintf/sprintf: format spec. incompatible with data - "9"
    *Error* load: error while loading file - "/home/samiran/Desktop/
    test.ocn"


    Could you please look into this?


    Thanks & Regards,
    Samiran
     
    Samiran Dam, Aug 31, 2010
    #3
  4. Samiran Dam

    Marcel Preda Guest

    Hi Samiran,

    In the lists each element is a string, they were not yet "converted"
    to numbers.
    And you have used "%g" format, it is for floating numbers.
    Use
    fprintf(fout "%s " num)

    BR,
    Marcel
     
    Marcel Preda, Aug 31, 2010
    #4
  5. You could always use lineread() instead of gets() to read each line from the
    file. This will read them using the SKILL parser. So if you do:

    procedure( My_readDataFile(fileName)
    let( (pIn data retList)
    pIn = infile(fileName)
    while(data=lineread(pIn)
    unless(data==t
    retList=tconc(retList data)
    )
    )
    close(pIn)
    car(retList)
    )
    )

    Because I used lineread, the numbers get read as integers. Of course, if you
    then tried to print them using %g, you'd still have a problem, because integers
    are not floats (could use %n instead though).

    I also used tconc to avoid reversing the list.

    Regards,

    Andrew.
     
    Andrew Beckett, Sep 1, 2010
    #5
  6. Samiran Dam

    Samiran Guest

    Hi,

    In my situation, the numbers are not necessarily be integers..it can
    also be floating point numbers. Is there any method available to
    convert string to floats?

    Andrew,

    What is "car(retList)" processing?

    I am trying to use your code (....input data are integers only) in
    following manner:

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    fout=outfile("/home/samiran/Desktop/output.txt")

    let((pIn data retList)
    pIn = infile("/home/samiran/Desktop/input.txt")
    while(data=lineread(pIn)
    unless(data==t
    retList=tconc(retList data)
    )
    )
    close(pIn)
    car(retList)

    foreach(list1 retList
    foreach(num list1
    fprintf(fout "%n " num)
    )
    newline(fout)
    )
    )

    newline(fout)
    close(fout)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    The error is:

    *Error* fprintf/sprintf: format spec. incompatible with data - (1 2 3
    4)



    BR
    Samiran
     
    Samiran, Sep 2, 2010
    #6
  7. Samiran Dam

    Samiran Guest

    How can I access a list element-by-element? I mean can I convert a
    list to an array? Using foreach(...) gives me the element values to
    the same variable, but I want like this - suppose if a list contains N
    elements, then I want 1st element to be stored into 'var1', 2nd to be
    stored into 'var2' and so on.


    BR
    Samiran
     
    Samiran, Sep 2, 2010
    #7
  8. Samiran Dam

    Samiran Guest

    Andrew & Marcel,


    Thanks for the help. I have figured out how to do.....your suggestions
    really helped me a lot!


    BR
    Samiran
     
    Samiran, Sep 2, 2010
    #8
  9. Samiran wrote, on 09/02/10 05:42:
    Because the list is a "tconc" structure, the actual list you're interested in is
    car(retList) - look at the documentation for tconc.

    If you're going to add printing in the same function, you would probably want to do:

    retList=car(retList)

    and then continue. The statement without the assignment worked because it was
    the last statement in the let and hence was the return value.

    Regards,

    Andrew.
     
    Andrew Beckett, Sep 2, 2010
    #9
  10. Samiran wrote, on 09/02/10 07:44:
    Do you really want to do that? The whole point of a list is that it has a
    variable number of elements. Creating lots of variables such as this is a very
    non-LISP or non-SKILL way of doing things, and would not ben terribly efficient.

    Regards,

    Andrew.
     
    Andrew Beckett, Sep 2, 2010
    #10
  11. The evalstring() function might be what you want. From what I can
    tell, it doesn't automatically convert to float; it appears to detect
    float or integer based on the presence of a decimal point. Watch how
    it behaves in these command line examples:


    ocean> a="2"
    "2"
    ocean> evalstring(a)
    2
    ocean> evalstring(a)/2
    1
    ocean> evalstring(a)/3
    0

    There, you can see that the result of evalstring(a) was treated as an
    integer. But if the original string has a decimal point:

    ocean> a="2.0"
    "2.0"
    ocean> evalstring(a)/3
    0.6666667

    then the result of evalstring(a) will be a float.

    Best,
    Stephen Greenwood
     
    Stephen Greenwood, Sep 13, 2010
    #11
  12. Stephen Greenwood wrote, on 09/13/10 22:05:
    Stephen,

    I never like using evalstring() for things like this - what if the string you
    had accidentally contained the string "exit()" ?

    Instead you could use atof() to convert the string to a float. Or if you want to
    handle engineering suffixes (e.g. "6u") then you could use cdfParseFloatString().

    Regards,

    Andrew.
     
    Andrew Beckett, Sep 15, 2010
    #12
  13. Thanks, Andrew. I haven't had any problems with evalstring(), since
    I've always had control over its argument, but you're right, atof()
    and cdfParseFloatString() are more robust. Thanks for pointing them
    out.

    Best Regards,
    Stephen
     
    Stephen Greenwood, Sep 16, 2010
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.