• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Write String to a File question

Stats

  • Locked Locked
  • Replies 10
  • Subscribers 144
  • Views 17758
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Write String to a File question

netbug
netbug over 9 years ago

Dear all,

This is a simple question.

I am using a form to collect data entered by the user. I would like to store this data into a text file, separating each field into different lines in the text file.

I have come up with a solution that is far away from being the best. I have stored all the information into a string and then I've parsed into a list. After that each element of list is written into the file in separate lines.

Please find my code below. In this case I am already passing the list and not the string.

Thanks in advance,

Pedro

procedure(WriteToFile(myPath myList)
let((myPort FileExists txtStr delimiter)
FileExists=isFileName(myPath) ; Test if the file exists

; Check if config file exists
if((FileExists==t) then
printf("File Exists !")
else
myPort=outfile( myPath ) ; open file for write
for( i 0 length(myList)-1 ; loop through the list
str=nth(i myList)
fprintf(myPort "%s\n" str) ; write string to file
)
close( myPort ) ; close file
printf("File Created !")
); end if
); End let
); End procedure

 

  • Cancel
  • JayR
    JayR over 9 years ago
    The code looks reasonable to me. That code supports a dynamic number of list elements. I just think you need to add a couple lines of error-checking: 1) assert(portp(myPort)) and 2) assert(stringp(str)) .
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • netbug
    netbug over 9 years ago
    Thanks for your answer. I will include the error checking instructions then. :-)
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 9 years ago

    I disagree. The code is using a for loop to iterate over a list, and then nth() to access each element. If the list isn't too long, that's OK, but it's a bad paradigm to start using - it will lead you into trouble. The reason being that lists are sequential, so doing this is order of N^2 (each time you use nth it has to start counting from the beginning of the linked list). It would be better to use foreach() over the list, or you could use pprint() to write the list out in one go - and then lineread to read it back in again. 

    You might also want to look at which gives an example of saving info from a form and loading it again later.

    Regards,

    Andrew

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • netbug
    netbug over 9 years ago

    Good morning,

    Thanks Andrew for your answer.

    Briefly, what I am doing is a script that when runs for the first time, it asks the user to fill in some fields. After that it stores all the information into a text file (config file). Then whenever the script runs again it checks if the config file already exists or not.

    As I have very little experience with SKILL I write a lot of code that is not efficient. I will take a look at your code and redo my own script.

    Thanks for your feedback,

    José

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • JayR
    JayR over 9 years ago
    Usually with lists it is better to use foreach() than for(), but that is because the code is simpler. The performance only starts to really matter when your lists are 100 elements long.
    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 9 years ago

    Unknown said:
    Usually with lists it is better to use foreach() than for(), but that is because the code is simpler. The performance only starts to really matter when your lists are 100 elements long.

    Why use an approach that is more complex and requires more code and when you suddenly find yourself dealing with larger lists than you anticipate will get slow very quickly? Yes, for short lists it tends to be insignificant, but then it depends on how many times you are calling the piece of code that is doing this. 

    Much better to get into a good habit in the first place rather than writing code that is stylistically worse and also liable to be inefficient.

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • rish87
    rish87 over 6 years ago in reply to Andrew Beckett

    Hi Andrew,

    I would like to open a file in append mode and then write some variables value to the starting of the file(there are some texts exists in the starting of the file).

    Is there any function to write the file from starting? I don't want  to read the file and store it somewhere and again rewrite the file.

    Thank you

    Harish 

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • mbracht
    mbracht over 6 years ago in reply to rish87

    Hi Harish,

    I don't think this is possible - and it is not a limitation of SKILL but of the Unix filesystem. So you need to recreate the file...

    Max

    • Cancel
    • Vote Up +1 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 6 years ago in reply to rish87

    Hi Harish,

    You mean insert some text at the beginning of the file, keeping the remainder? The simple answer is no, because even in the basics of the operating system this isn't possible (it would be expensive, because it would involve having to copy the contents, write the new lines, and then paste again). Even if it had been possible to use fileSeek to move to the beginning of the file and then print from there, it would overwrite the lines at that position - but that's not possible.

    See this article: Can I use SKILL fprintf command to append a line in the middle or beginning of a file instead of end of file? No.

    See also this which discusses the limitations of fseek with files opened in append mode in C: https://stackoverflow.com/questions/46892945/initial-value-of-file-pointer-position-returned-by-ftell-in-append-mode?rq=1

    It would be easy enough to read and store the file (I'm using /tmp/stuff as my file I want to read and write). Here I use outstring() as a convenient way of reading and writing to an in-memory file which I can then grab the contents of in one go.

    in=infile("/tmp/stuff")
    out=outstring()
    while(gets(line in) fprintf(out "%s" line))
    close(in)
    entireFile=getOutstring(out)
    close(out)

    out=outfile("/tmp/stuff")
    fprintf(out "New content\n")
    fprintf(out "%s" entireFile)
    close(out)

    Regards,

    Andrew.

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • rish87
    rish87 over 6 years ago in reply to Andrew Beckett

    Thanks for your quick reply Andrew,

    it helped a lot!

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information