c语言中write函数 c语言write函数向文件写入字符串

write写出常见的输入格式有

write写出常见的输入格式为f.write()。write函数是C语言函数。C语言函数是一种函数,用来编译C语言,所在库函数为ctype.h,分为分类函数,数学函数,目录函数,进程函数,诊断函数,操作函数等。

成都创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站制作、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的鄠邑网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

C语言write函数的返回值问题

大多数情况下,write成功后返回的写入字节数都等于你传入的长度。

但是如果你要写的长度超过了的文件的最大可能时,比方说,你的磁盘还剩下128个字节,这时你向磁盘上的某个文件一次性写512个字节,返回值就是128,只有前128个字节成功写入。

再比如,你用write写的不是一个普通文件,而是设备文件/socket等,那也可能返回值小于你指定的值,这就可能是具体设备的限制等,比如写入的数量超过了缓冲大小等。

在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[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和read语句的基本用法

1、函数名: write

表头文件:#includeunistd.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非有效的文件描述词,或该文件已关闭。

程序例:

#includestdlib.h

#includeunistd.h

#includestdio.h

#includestring.h

#includefcntl.h

#includeerrno.h

intmain(void)

{

inthandle;

charstring[40];

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[5];/*ReservedforDOS*/

charxfcb_attr;/*Searchattribute*/

structfcbxfcb_fcb;/*Thestandardfcb*/

};

2、函数名: read

表头文件:#includeunistd.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;


网站题目:c语言中write函数 c语言write函数向文件写入字符串
网站链接:http://pwwzsj.com/article/ddeejgj.html