1
$\begingroup$

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?

$\endgroup$
16
  • 1
    $\begingroup$ With versions 8.0.4 and 10.4 on Win7 x64 I get "hello \t bye" in the both cases. $\endgroup$ Mar 17, 2016 at 12:12
  • $\begingroup$ not sure if it matters, but for comparison you should Close["myFile"] $\endgroup$
    – george2079
    Mar 17, 2016 at 12:33
  • $\begingroup$ @AlexeyPopkov I suppose it should be noted that ReadString is not available before version 10, but one can of course use another utility for this. $\endgroup$ Mar 17, 2016 at 12:35
  • $\begingroup$ With version 8 I used Import[..., "Text"] instead of ReadString. $\endgroup$ Mar 17, 2016 at 12:36
  • $\begingroup$ @george2079 I'm sorry, what will Close["myFile"] achieve? $\endgroup$ Mar 17, 2016 at 12:38

1 Answer 1

0
$\begingroup$

This isn't much of an answer, but I don't want the question to show up as unanswered.


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).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.