1

I've read in a text file, which in the global env, looks good:

enter image description here

Note: I am on windows, and the linebreaks are \r\n.

It looks like it was read in properly, because if I look at the textfile in notepad++ I see the symbols:

enter image description here

However, if I try to write the file back using cat, I get additional {CR} bytes thrown in:

cat(txtFile, file="out.txt", append="FALSE")

enter image description here

This leads to the resulting text file appearing 'double spaced.' Very annoying, and I don't know why cat() is doing that. I've also tried writeLines, but any time it tries to write the string to the file, it just puts the {CR}'s before the {CRLF}.

Does anyone know how to stop this behavior?

Chris Knoll
  • 391
  • 2
  • 13
  • read up `?cat`. cat by DEFAULT adds a sep = " ". try your code with `cat(txtFile, file="out.txt", append="FALSE", sep ="")` and see if that meets your expectations – infominer May 17 '19 at 20:42
  • That didn't work. I believe sep="" will be used to concatenate vectors or lists of strings together with a separator. In this case, it was a single string, read from a file. I want to just write out the read-in string back, and I expect read-in -> write-out on an unmodified string value should result in the same output, but it does not. – Chris Knoll May 20 '19 at 04:49

1 Answers1

1

The solution here was similar to the answer found here: R: Getting Unix-like linebreak LF writing files with cat()

    f<-file("out.txt", open="wb");
    cat(txtFile,file=f,sep="");
    close(f);

Resulting text file output in notepad++: enter image description here

Chris Knoll
  • 391
  • 2
  • 13