C语言中的Write函数

我想知道一个问题,那就是Write函数所需要的参数handle的值与Write函数的效果之间有什么关系。handle的值我通常见别人用handle=open("xxx",... 我想知道一个问题,那就是Write函数所需要的参数handle的值与Write函数的效果之间有什么关系。 handle的值我通常见别人用handle = open("xxx", O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE))来取得,我不知道这里究竟是判断什么来取得handle的值的。 handle的值在write函数中究竟有什么作用?
write(1,"string",15); 为什么能够与printf的效果相同,这里的1的位置就是handle的位置,handle是否能看作“功能选择子(Func descriptor)”呢? handle是一个术语, 它一般情况下究竟是做什么用的? 望高手赐教。
展开
 我来答
花式码农
2007-11-04 · TA获得超过1.7万个赞
知道大有可为答主
回答量:4894
采纳率:0%
帮助的人:4845万
展开全部
//摘抄自MSDN。这是write的替代品。
Writes data to a file.

int _write(
int fd,
const void *buffer,
unsigned int count
);

Parameters
fd
File descriptor of file into which data is written.

buffer
Data to be written.

count
Number of bytes.

Return Value
If successful, _write returns the number of bytes actually written. If the actual space remaining on the disk is less than the size of the buffer the function is trying to write to the disk, _write fails and does not flush any of the buffer's contents to the disk. A return value of –1 indicates an error. If invalid parameters are passed, this function invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, the function returns -1 and errno is set to one of three values: EBADF, which means the file descriptor is invalid or the file is not opened for writing; ENOSPC, which means there is not enough space left on the device for the operation; or EINVAL, which means that buffer was a null pointer.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, return codes.

If the file is opened in text mode, each linefeed character is replaced with a carriage return – linefeed pair in the output. The replacement does not affect the return value.

Remarks
The _write function writes count bytes from buffer into the file associated with fd. The write operation begins at the current position of the file pointer (if any) associated with the given file. If the file is open for appending, the operation begins at the current end of the file. After the write operation, the file pointer is increased by the number of bytes actually written.

When writing to files opened in text mode, _write treats a CTRL+Z character as the logical end-of-file. When writing to a device, _write treats a CTRL+Z character in the buffer as an output terminator.

Requirements
Routine Required header Compatibility
_write
<io.h>
Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

Example
Copy Code
// crt__write.c
//
// This program opens a file for output and uses _write to write
// some bytes to the file.

#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <share.h>

char buffer[] = "This is a test of '_write' function";

int main( void )
{
int fileHandle = 0;
unsigned bytesWritten = 0;

if ( _sopen_s(&fileHandle, "write.o", _O_RDWR | _O_CREAT,
_SH_DENYNO, _S_IREAD | _S_IWRITE) )
return -1;

if (( bytesWritten = _write( fileHandle, buffer, sizeof( buffer ))) == -1 )
{
switch(errno)
{
case EBADF:
perror("Bad file descriptor!");
break;
case ENOSPC:
perror("No space left on device!");
break;
case EINVAL:
perror("Invalid parameter: buffer was NULL!");
break;
default:
// An unrelated error occured
perror("Unexpected error!");
}
}
else
{
printf_s( "Wrote %u bytes to file.\n", bytesWritten );
}
_close( fileHandle );
}

Output

Wrote 36 bytes to file.

See Also
Reference
Low-Level I/O
fwrite
_open, _wopen
_read
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
艳阳高照的午后
推荐于2016-12-01 · TA获得超过1万个赞
知道大有可为答主
回答量:1.2万
采纳率:97%
帮助的人:5335万
展开全部
  Write函数
  用法:
  write函数所在的头文件为 <unistd.h>
  write有两种用法。一种是:
  ssize_twrite(int handle, void *buf, int nbyte);
  handle 是文件描述符;
  buf是指定的缓冲区,即指针,指向一段内存单元;
  nbyte是要写入文件指定的字节数;返回值:写入文档的字节数(成功);-1(出错)
  write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.
  另一种是:write(const char* str,int n)
  str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
  write("string",strlen("string");表示输出字符串常量
  
  程序示例:

  #include <stdio.h>
  #include <stdlib.h>
  #include <fcntl.h>
  #include <sys\stat.h>
  #include <io.h>
  #include <string.h>
  int main(void)
  {
  int *handle; char string[40];
  int length, res;/* Create a file named "TEST.$$$" in the current directory and write a string to it. If "TEST.$$$" already exists, it will be overwritten. */
  if ((handle = open("TEST.$$$", O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE)) == -1)
  {
  printf("Error opening file.\n");
  exit(1);
  }
  strcpy(string, "Hello, world!\n");
  length = strlen(string);
  if ((res = write(handle, string, length)) != length)
  {
  printf("Error writing to the file.\n");
  exit(1);
  }
  printf("Wrote %d bytes to the file.\n", res);
  close(handle); return 0; }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式