Questions tagged [writefile]

For questions about writing to a file. It could be referencing appending data, rewriting all existing content, or other related uses of functions that programmatically edit files. It is not limited to a certain language.

This tag is most often used when someone wants to add/insert content to a file programmatically. One example is in the Node JS function fs.writeFile, which is used to create files and write to them.

This is an example script which shows the usage of fs.writeFile to write a string to message.txt.

// Node JS

const fs = require('fs');
const data = new Uint8Array(Buffer.from('Hello Node.js'));

fs.writeFile('message.txt', data, (err) => {
  if (err) throw err;
  console.log('The file has been saved!');
});

Make sure to pair this tag with a language tag as is generally not specific enough to describe a complete question.

494 questions
19
votes
1 answer

writeFile does not create file

Error code looks like: { Error: ENOENT: no such file or directory, open 'sad' errno: -2, code: 'ENOENT', syscall: 'open', path: 'sad' } where 'sad' is the name of file I would like to write to and it doesn't exist. Code looks like…
SubjectX
  • 836
  • 2
  • 9
  • 33
14
votes
2 answers

Why does WriteFile crash when writing to the standard output?

Here's a "Hello world" program that uses WinAPI's WriteFile (compiled in Microsoft Visual C++ 2008 Express): int _tmain(int argc, _TCHAR* argv[]) { wchar_t str[] = L"Hello world"; HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE); if(out &&…
user38329
  • 719
  • 4
  • 17
13
votes
1 answer

fs.writeFile not overwriting file

I am using node-crontab to run the script. fs.writeFile overwrite first time the loop runs but after that is appending data. I have tried deleting the file before writing but is doing the same, deletes it the first time but in the subsequent runs…
Vanessa Torres
  • 169
  • 1
  • 2
  • 9
12
votes
3 answers

Save byte array to file node JS

I want to save bytearray to a file in node js, for android I'm using the below code sample, can anyone suggest me the similar approach File file = new File(root, System.currentTimeMillis() + ".jpg"); if (file.exists()) …
Anuj J Pandey
  • 656
  • 1
  • 4
  • 17
11
votes
4 answers

Writing JSON object to .json file on server

I'm trying to write my JSON object to a .json file on the server. The way I'm doing this now is: JavaScript: function createJsonFile() { var jsonObject = { "metros" : [], "routes" : [] }; // write cities to JSON Object …
Hristo
  • 45,559
  • 65
  • 163
  • 230
9
votes
1 answer

What does os.O_TRUNC do?

func OpenFile(name string, flag int, perm FileMode) (*File, error) f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE|O_TRUNC, 0755) Does "O_TRUNC" empty the entire file before writing? What do they mean with "truncates"? Also, does the…
user12316120
9
votes
2 answers

Why does this not run in constant memory?

I am trying to write a very large amount of data to a file in constant memory. import qualified Data.ByteString.Lazy as B {- Creates and writes num grids of dimensions aa x aa -} writeGrids :: Int -> Int -> IO () writeGrids num aa = do rng <-…
Justin Raymond
  • 3,413
  • 2
  • 19
  • 28
7
votes
1 answer

numpy array2string applied on huge array, skips central values, ( ... in the middle )

I have array of size (3, 3, 19, 19), which I applied flatten to get array of size 3249. I had to write these values to file along with some other data, so I did following to get the array in string. np.array2string(arr.flatten(), separator=', ',…
Brandon Lee
  • 695
  • 1
  • 10
  • 22
7
votes
1 answer

Is WriteFile atomic?

I'm designing a system that will write time series data to a file. The data is blocks of 8 bytes divided into two 4 bytes parts, time and payload. According to MSDN the WriteFile function is atomic (…
ROAR
  • 1,304
  • 1
  • 11
  • 28
6
votes
4 answers

Save Java txt file to folder

First of all - I love you all at Stackoverflow! Everyone is very helpful! Sadly when I go to answer questions they are all too advance for me :'( I want to save the text file to a folder - but not an absolute folder for example I want to save it to…
Rabiani
  • 143
  • 2
  • 5
  • 12
6
votes
1 answer

problem writing a NSMutableArray to file in cocoa

A real beginners question. I have a NSView subclass in which I create a NSMutableArray containing NSValues. When I want to write the array to a file using writetofile:atomatically: the file is created but it contains none of the NSValues that the…
Luuk
6
votes
2 answers

Windows - safe to call CloseHandle() when an Overlapped I/O is in progress?

I have a program that has multiple threads which need to use the same handle (it is opened with FILE_SHARE_READ) to write. What happens if one thread closes the handle while another currently has an I/O in progress (WriteFile, in my case)? Does…
user2563087
  • 151
  • 1
  • 8
6
votes
2 answers

json file I/O with pretty print format using gson in java?

I already know how gson works and also know how to enable pretty print. I want to use gson and not simplejson. The problem I had is that I wasn't able to create a file consisting of a List of Employee objects. I've seen every other java threads in…
MoeBoeMe
  • 133
  • 1
  • 8
6
votes
1 answer

Write each element of a list on a newline on a text file in Python

Having read this question and this question, I am trying to write each element of a list (mylist) on a newline on a text file (text.txt). So, for example, the list mylist = ['a', 'b', 'ccc', 'dd', 'eeee', 'f', 'ggg'] should be written on the…
user6003691
6
votes
2 answers

How to write a only integers numpy 2D array on a txt file

I know how to write the txt file using numpy.savetxt() but I can't get it to write the file using just integers. I have the following code: new_picks = new_picks.astype(int) np.savetxt(fname='newPicksData.txt', X=new_picks.astype(int)) This is how…
Diego Aguado
  • 1,604
  • 18
  • 36
1
2 3
32 33