Appending to a File

In the eggPlant Documentation, under working with files, it is a possibility to write a file as so:

add 1 to item 2 of line 1 of file "/tmp/testing/counters"

In this example, it is not written that the file is being stored into a container before being written to. My question is then, is it possible to write to a file without having to store its contents into a container in eggPlant? And if so, would the syntax follow as above?

Comments

  • SenseTalkDougSenseTalkDoug ForumAdmin admin
    I'm not sure I understand exactly what you are asking. The add command that you copied above illustrates the fact that a file can be treated directly as a container in SenseTalk (like a variable). That single line of code will modify the contents of the second item of the first line of the file, without needing to first read the file into a variable, then modify that and write it back out.

    So the specific answers to your questions are: Yes, you can write to a file without storing it to some other container first; and Yes, the line of code you showed is one example of how to do that.

    Is that what you wanted to know?
  • Your answer helps clarify some of my confusion. Thank you. What I was trying to ask is if there is a way to append to a file without having to load that file into eggPlant memory?
  • EggplantMattEggplantMatt ForumAdmin admin
    If you just want to append to the file, you can use code like this:
    put return & "very important data" after file "/path/to/myfile"
    
    This does not load the file into memory; neither does the example in your original post. You can also "put before" a file or put something into a specific line. eggPlant opens the file, modifies it, and closes it.
Sign In or Register to comment.