作为node.js文档
fs.write(fd,缓冲器,偏移量,长度,位置,回调)
所以我写到:
var fs = require('fs');
fs.open('./example.txt', 'a', 0666, function(err, fd) {
if (err) { throw err; }
console.log('file opened');
fs.write(fd, 'test', null, null, null, function(err) {
if (err) { throw err; }
console.log('file written');
fs.close(fd, function() {
console.log('file closed');
});
});
});但是fs.write的回调不会被触发。输出只是“文件翻转”。
fs.write(fd, 'test', null, null, function(err) {但我为第5个参数而不是第6个参数分配回调。这是工作。为什么与文件不同。
在节点源( node_file.cc )中,回调是第6个参数。
Local<Value> cb = args[5];我不明白。
发布于 2011-10-08 19:02:38
fs.write有一个更老的接口,现在仍然支持它。它允许写字符串。因为您提供了一个字符串,而不是一个“缓冲区”节点,所以试图使您的参数适合这个较旧的接口:
fs.write(fd, data, position, encoding, callback)注意,旧的接口有‘回调’作为第五个参数。对于第五个参数,您给出了'null‘:
fs.write(fd, 'test', null, null, null, function(err) {节点看到了您的回调的“null”,因此不认为您已经给节点一个回调。
或者按照建议使用缓冲区数据字符串,或者正确地使用旧接口来使用普通字符串。如果您现在还没有准备好使用缓冲区,只需使用“新缓冲区(‘test’)”,直到准备就绪。
发布于 2011-10-08 16:14:06
您需要将一个缓冲器,而不是一个字符串作为第二个参数传递给fs.write。此外,回调将给出三个参数,而不是一个:
var buffer = new Buffer('test');
fs.write(fd, buffer, 0, buffer.length, null, function(err, written, buffer) {来自文档
fs.write(fd,缓冲器,偏移量,长度,位置,回调) 将缓冲区写入fd指定的文件。 偏移量和长度决定要写入的缓冲区的部分。 位置是指从文件开头开始的偏移量,该偏移量应该写入该数据。如果位置为null,则数据将写入当前位置。见pwrite(2)。 回调将被赋予三个参数(err,written,buffer),其中写的指定从缓冲区写入的字节数。
最后,调用fs.open中的'0666‘表示一个UNIX文件模式。
https://stackoverflow.com/questions/7696235
复制相似问题