c write函数(C语言中的Write函数)

本文目录
C语言中的Write函数
write()写文件函数
原形:int write(int handle,char *buf,unsigned len)
功能:将缓冲区的数据写入与handle相联的文件或设备中,handle是从creat、open、dup或dup2调用中得到的文件句柄。
对于磁盘或磁盘文件,写操作从当前文件指针处开始,对于用O_APPEND选项打开的文件,写数据之前,文件指针指向EOF;对于设备,字节被直接传送到设备中。
扩展资料:
用法
头文件:《unistd.h》
write有两种用法。一种是:ssize_t write(int fd, const void *buf, size_t nbyte);
fd:文件描述符;
buf:指定的缓冲区,即指针,指向一段内存单元;
nbyte:要写入文件指定的字节数;
返回值:写入文档的字节数(成功);-1(出错)
write函数把buf中nbyte写入文件描述符handle所指的文档,成功时返回写的字节数,错误时返回-1.
另一种是: write(const char* str,int n)
str是字符指针或字符数组,用来存放一个字符串。n是int型数,它用来表示输出显示字符串中字符的个数。
write("string",strlen("string");表示输出字符串常量
write写出常见的输入格式有
write写出常见的输入格式为f.write()。write函数是C语言函数。C语言函数是一种函数,用来编译C语言,所在库函数为ctype.h,分为分类函数,数学函数,目录函数,进程函数,诊断函数,操作函数等。
c语言 write()、read()函数原型
把指定数量的数据写入文件,若成功则返回写入的字节数,否则返回-1
int _write( int handle, const void *buffer, unsigned int count );
这个例子中把字符串“hello”写入文件sample.txt中:
int Handle; char String="hello";
Handle = _open("sample.txt",_O_RDWR|_O_CREAT);
_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;
if( (fh = _open( "eof.c", _O_RDONLY )) == - 1 ) {
perror( "Open failed");
exit( 1 );
}
while( !_eof( fh ) ) {
if( (count = _read( fh, buf, 10 )) == -1 ) {
perror( "Read error" );
break;
}
total += count;
}
printf( "Number of bytes read = %d\n", total );
_close( fh );
}
在C语言中要用到write和read函数要用到什么头文件
1、要用到unistd.h头文件。
2、 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");表示输出字符串常量
3、程序示例:
#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;
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和read语句的基本用法
1、函数名: write
表头文件:#include《unistd.h》
定义函数:ssize_t write (int fd,const void * buf,size_t count);
函数说明:write()会把指针buf所指的内存写入count个字节到参数fd所指的文件内。当然,文件读写位置也会随之移动。
返回值:如果顺利write()会返回实际写入的字节数。当有错误发生时则返回-1,错误代码存入errno中。
错误代码:
EINTR 此调用被信号所中断。
EAGAIN 当使用不可阻断I/O 时(O_NONBLOCK),若无数据可读取则返回此值。
EBADF 参数fd非有效的文件描述词,或该文件已关闭。
程序例:
#include《stdlib.h》
#include《unistd.h》
#include《stdio.h》
#include《string.h》
#include《fcntl.h》
#include《errno.h》
intmain(void)
{
inthandle;
charstring;
intlength,res;
/*
Createafilenamed"TEST.$$$"inthecurrentdirectoryandwrite
astringtoit.If"TEST.$$$"alreadyexists,itwillbeoverwritten.
*/
if((handle=open("TEST.$$$",O_WRONLY|O_CREAT|O_TRUNC,
S_IREAD|S_IWRITE))==-1)
{
printf("Erroropeningfile.\n");
exit(1);
}
strcpy(string,"Hello,world!\n");
length=strlen(string);
if((res=write(handle,string,length))!=length)
{
printf("Errorwritingtothefile.\n");
exit(1);
}
printf("Wrote%dbytestothefile.\n",res);
close(handle);
return0;
}
structxfcb{
charxfcb_flag;/*Contains0xfftoindicatexfcb*/
charxfcb_resv;/*ReservedforDOS*/
charxfcb_attr;/*Searchattribute*/
structfcbxfcb_fcb;/*Thestandardfcb*/
};
2、函数名: read
表头文件:#include《unistd.h》
定义函数:ssize_t read(int fd,void * buf ,size_t count);
函数说明:read()会把参数fd 所指的文件传送count个字节到buf指针所指的内存中。若参数count为0,则read为实际读取到的字节数,如果返回0,表示已到达文件尾或是无可读取的数据,此外文件读写位置会随读取到的字节移动。
附加说明:如果顺利read()会返回实际读到的字节数,最好能将返回值与参数count 作比较,若返回的字节数比要求读取的字节数少,则有可能读到了文件尾、从管道(pipe)或终端机读取,或者是read()被信号中断了读取动作。当有错误发生时则返回-1,错误代码存入errno中,而文件读写位置则无法预期。
错误代码:
EINTR 此调用被信号所中断。
EAGAIN 当使用不可阻断I/O 时(O_NONBLOCK),若无数据可读取则返回此值。
EBADF 参数fd 非有效的文件描述词,或该文件已关闭。
程序例:
#include
#include
#include
#include
#include
#include
int main(void)
{
void *buf;
int handle, bytes;
buf = malloc(10);
/*
Looks for a file in the current directory named TEST.$$$ and attempts
to read 10 bytes from it. To
}
if ((bytes = read(handle, buf, 10)) == -1) {
printf("Read Failed.\n");
exit(1);
}
else {
printf("Read: %d bytes read.\n", bytes);
}
return 0;

更多文章:
在from子句中可以出现(如何在from 子句中嵌套查询下面的语句在access中出错!)
2026年10月11日 05:20
countif函数统计个数怎么用(countif函数怎么用 详解Excel中countif函数的使用方法)
2026年10月11日 03:30
正则匹配数字之前的字符(正则表达式如何匹配前面是数字、中间是“/”、后面也是数字,就像2/3专业的模式)
2026年10月11日 03:00
orlnsertbootmediinselected(我电脑开机显示这个是什么意思or insert boot media in select)
2026年10月10日 23:00
display的用法(display是什么意思 详解display的含义和用法)
2026年10月10日 22:00
html全部居中代码(怎么让网页居中显示,html如何让网页居中)
2026年10月10日 21:10





