Assuming I already have the file created.
I have a programme that,returns an output, in our case number. I want Mathematica to save this number in a file, together with results saved from previous calculations.
After having checked Mathematica commands, I found that I could do something like:
file = OpenWrite["test.dat"];
Write[file, new];
Close[file];
However, this doesn't work, because it overwrites the information that is already in the file. Working with this limitation, I tried a different approach:
results=Import["test.dat", "Table"];
AppendTo[results,newresult];
file = OpenWrite["test.dat"];
Write[file,results];
Close[file];
This new approach still doesn't work since when I initialize the file test.dat with
Export["test.dat", Range[1, 10]]
and then import the list, I do not get the same list, but
{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}}
I get the same unwanted list, even if I add the "Table" option to Export, or Import...
How do I efficiently save the progress of my programme in the file test.dat?
Any help would be appreciated.
Put
andPutAppend
$\endgroup$(# >>> "test.dat") & /@ {1, 2, 3}
followed byReadList["test.dat"]
. For me, this returns{1,2,3}
. If you now want to add some new numbers, say(# >>> "test.dat") & /@ {4, 5, 6, 7, 8}
, you will find thatReadList["test.dat"]
gives you{1,2,3,4,5,6,7,8}
. If you only need to save numbers/lists, and not expressions, then this is the simplest way I can think of. $\endgroup$>>>
? Try starting from a new file name. $\endgroup$