-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add std.writeFile #932
Add std.writeFile #932
Conversation
std.loadFile already exists and is convenient. std.writeFile replaces the more onerous std.open + std.write + std.close idiom.
When `data` is a string, the file is opened in text mode; otherwise it is | ||
opened in binary mode. (This distinction is only relevant on Windows.) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might add that the string is encoded in UTF-8 on all systems, whereas the data from typed arrays or ArrayBuffer
objects is simply written as untranslated binary data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I'm not entirely sure that's correct. It uses fwrite and on Windows that's code page-dependent, AFAIK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I'm not entirely sure that's correct. It uses fwrite and on Windows that's code page-dependent, AFAIK.
Indeed fwrite
in Windows can do this, so if the stream is configured for code page or worse Unicode conversion, fwrite
would unlikely produce gibberish output, aka Mojibake.
Here are the relevant paragraphs in the Microsoft help page:
The
fwrite
function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there's one) is incremented by the number of bytesfwrite
writes. If stream is opened in text mode, each line feed is replaced with a carriage return-line feed pair. The replacement has no effect on the return value.
When stream is opened in Unicode translation mode — for example, if stream is opened by calling
fopen
and using a mode parameter that includesccs=UNICODE
,ccs=UTF-16LE
, orccs=UTF-8
, or if the mode is changed to a Unicode translation mode by using_setmode
and a mode parameter that includes_O_WTEXT
,_O_U16TEXT
, or_O_U8TEXT
— buffer is interpreted as a pointer to an array ofwchar_t
that contains UTF-16 data. An attempt to write an odd number of bytes in this mode causes a parameter validation error.
You open the file in default mode (text mode would use "wt"
) so the stream translation mode will be the default translation mode defined by the global variable _fmode which could be _O_TEXT
and trigger LF to CR/LF translation or _O_BINARY
which would leave the output unchanged.
Unless I am missing some more subtle side effects, the data written should just undergo line ending translation.
My suggestion is correct for unix-like systems and somewhat helpful, albeit UTF-8 is probably expected on these systems, and a more explicit caveat for Windows could help programmers who set up the standard streams stdout
or stderr
to perform code page or Unicode translation.
std.loadFile already exists and is convenient. std.writeFile replaces the more onerous std.open + std.write + std.close idiom.