Is there a way to wrote a single opening quote to a file with -file wrote-?
In a program I have the command
file write texfile `"`line'"'
which wrotes the contents of the local macro `line' to the file handle
"texfile". The command works well except for lines which contains either a
local macro name (like `a') , or a single opening quote. In the first case
Stata expands the local macro name to the contents of the local macro. But I
want to have the local macro name (`a') in my file handle. In the second case
I got an error message because Stata thinks that there should be a valid
local macro name, which is not there.
I tried to escape each occurence of the single opening quote with the
backslash by substituting ` with \`. But
local line: subinstr local line `"`"' `"\`"'
does not work for obvious reasons. What else could be done?
I had essentially the same problem when I was writing my -intext- package
(now downloadable from SSC - type -ssc describe intext-). The problem was
discussed in a thread on Statalist, and a consensus seemed to emerge that
the solution, in Stata as we know it, is to use -file- in binary mode (byte
by byte) rather than text mode (line by line). You might like to look
inside my -intext.ado- to see how I handled the awkward fact that a line of
a text file is delimited by a linefeed under Unix, a carriage return under
MacOS, and a carriage return and a line feed under Windows.