C语言中的Write函数

我想知道一个问题,那就是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是一个术语, 它一般情况下究竟是做什么用的? 望高手赐教。

第1个回答  2007-11-04
//摘抄自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
第2个回答  推荐于2016-12-01
  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; }

C语言中的Write函数
write()写文件函数 原形:int write(int handle,char *buf,unsigned len)功能:将缓冲区的数据写入与handle相联的文件或设备中,handle是从creat、open、dup或dup2调用中得到的文件句柄。对于磁盘或磁盘文件,写操作从当前文件指针处开始,对于用O_APPEND选项打开的文件,写数据之前,文件指针指向EOF;对于...

C语言中的Write函数
C语言中的Write函数,是用于文件操作的重要工具。这个函数的原型为int write(int handle, char *buf, unsigned len),它的核心任务是将缓冲区的数据写入与handle关联的文件或设备中。handle通常在create、open、dup或dup2等函数调用后获取,代表文件句柄。当我们需要对磁盘或磁盘文件进行写入时,write操作...

在C语言中要用到write和read函数要用到什么头文件
1、要用到unistd.h头文件。2、 Write函数用法:write函数所在的头文件为 <unistd.h>write有两种用法。一种是:ssize_twrite(int handle, void *buf, int nbyte);handle 是文件描述符;buf是指定的缓冲区,即指针,指向一段内存单元;nbyte是要写入文件指定的字节数;返回值:写入文档的字节数(成...

C语言 write和read语句的基本用法
1、函数名: write 表头文件:#include<unistd.h> 定义函数:ssize_t write (int fd,const void * buf,size_t count);函数说明:write()会把指针buf所指的内存写入count个字节到参数fd所指的文件内。当然,文件读写位置也会随之移动。返回值:如果顺利write()会返回实际写入的字节数。当有错误发生时则...

C语言write函数的返回值问题
大多数情况下,write成功后返回的写入字节数都等于你传入的长度。但是如果你要写的长度超过了的文件的最大可能时,比方说,你的磁盘还剩下128个字节,这时你向磁盘上的某个文件一次性写512个字节,返回值就是128,只有前128个字节成功写入。再比如,你用write写的不是一个普通文件,而是设备文件\/socket...

c语言 write()、read()函数原型
_write(Handle,String,sizeof(String));_close(Handle);从一个文件读取数据 int _read( int handle, void *buffer, unsigned int count );这个例子中打开文件eof.c,每次读取10个字节,直到全部字节被读完为止,然后显示文件的长度:void main( void ) { int fh, count, total = 0;char buf[...

Linux系统下C语言read,write函数
在Linux系统中,C语言的read和write函数常用于实现文件操作,比如简单的文件复制。以下是一个基础示例,用于帮助理解这两个函数在实际应用中的用法。首先,理解main函数的参数至关重要。在Linux系统编程中,我们通常使用`int main(int argc, char *argv[])`,而不是仅`int main()`。参数`argc`代表...

c语言fwrite、fread、write、read的区别
在C语言中,fwrite()和fread()是标准库中处理文件读写的工具,而write()和read()则是系统级别的函数。它们的主要差异在于使用场景和底层机制。尽管fwrite()和fread()看似直接操作,但实际操作中会借助write()和read()来完成。为了优化性能,应尽量减少频繁的小规模文件操作,一次处理大量数据。1. ...

C语言中的read和write怎么用?
read和write是UNIX或者一些类UNIX系统,比如LINUX系统中使用的,称为LINUX系统函数。这种函数只能在特定的操作系统下使用,可移植性差。fread和fwrite是C库函数。这种函数基本在任何操作系统都能使用,可移植性高。2.基础知识介绍 只介绍LINUX系统函数,常用的有creat,open,close,read,write,lseek,access,...

C语言中的read和write怎么用?
read和write是UNIX或者一些类UNIX系统,比如LINUX系统中使用的,称为LINUX系统函数。这种函数只能在特定的操作系统下使用,可移植性差。fread和fwrite是C库函数。这种函数基本在任何操作系统都能使用,可移植性高。2.基础知识介绍 只介绍LINUX系统函数,常用的有creat,open,close,read,write,lseek,access,...

相似回答