I'm trying to write a function which takes some strings, manipulates them, and writes the result to a file.
Here's a small example which recreates the behavior I'm seeing:
writefile[instr1_, instr2_, filename_] :=
Module[{str1, outstr, stream},
str1 = "abc \n `` \n def \n ``";
outstr = OutputForm[StringForm[str1, instr1, instr2]];
Export[filename, outstr];
]
When I call writefile["xyz", "pqr", "test.txt"]
, I would expect test.txt to have the output:
abc
xyz
def
pqr
However, it instead looks like this:
abc xyz pqr
def
What's going on? I've tried the suggestions at this SE, but they all give the same result.
When I remove the OutputForm
call and simply return outstr
in the function above, it prints to the screen just fine, but writing this to file means that the output file reads literally StringForm["abc \n `` \n def \n ``", "xyz", "pqr"]
.