Important Note
A kernel or maybe Front End restart probably fixed the problem. I did something weird today which is evaluate Context[StringQ]="test`"
, perhaps that was the problem (I thought I quit the kernel after that, but maybe not). I am unable reproduce the error now. It may be difficult to track the problem down. I am using version 10.4 on OSX 10.10.
Original question
We can use WriteString
with a path or a stream as its first argument. There appears to be an inconsistency with how tabs are handled in these cases.
Note that WriteLine
appends to files, so for simplicity I will assume that we are writing to an empty file. I define a function which I think is supposed to be equivalent in this case to WriteString
with a path
as its first argument, as follows.
SetDirectory[$TemporaryDirectory];
writeStringUsingStream[path_, string_] :=
Module[
{stream},
stream = OpenWrite[path];
WriteString[stream, string];
Close@stream;
]
Now we test it. I use DeleteFile
because the comparison in case the file already exists is distracting/confusing/irrelevant.
string = "hello \t bye";
SetDirectory[$TemporaryDirectory];
If[FileExistsQ["myFile"], DeleteFile["myFile"]]
If[FileExistsQ["myFileUS"], DeleteFile["myFileUS"]]
WriteString["myFile", string]
writeStringUsingStream["myFileUS", string];
ReadString["myFile"] // FullForm
ReadString["myFileUS"] // FullForm
"hello \\[RawTab] bye" "hello \t bye"
Is this a bug? Or did I mess something up?
"hello \t bye"
in the both cases. $\endgroup$Close["myFile"]
$\endgroup$ReadString
is not available before version 10, but one can of course use another utility for this. $\endgroup$Import[..., "Text"]
instead ofReadString
. $\endgroup$Close["myFile"]
achieve? $\endgroup$