5

I have a result I want to save in a .txt file. So I use this line to save a result k:

param OutputFile symbolic := "output.txt";
print k >> (OutputFile);

However, I may run the script several times which means I will open an existing file and add lines to it. What I want to do is overwrite on the file output.txt so I will not have the previous runs results.

How do I do this?

SecretAgentMan
  • 1,895
  • 2
  • 13
  • 39
odd
  • 113
  • 3

2 Answers2

6

To overwrite a file that you have previously been writing in an AMPL session, first close the file:

close (OutputFile);

Then use > to overwrite the file:

print k > (OutputFile);

This deletes the file and then opens a new, empty file with the same name. Note that once the file is open, > and >> both append to it. A complete description of AMPL file redirection is given in section 12.7 General facilities for manipulating output of the AMPL book.

4er
  • 628
  • 3
  • 6
5

This should overwrite :

 k > (OutputFile);

While this appends :

 k >> (OutputFile);
Kuifje
  • 13,324
  • 1
  • 23
  • 56