当前位置:   article > 正文

c 语言write 函数,write - C 语言库函数手册

c语言中的write函数

write

能: 写到一文件中

法: int write(int

handel, void *buf, int nbyte);

程序例:

#include

#include

#include

#include

#include

#include

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;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/256375
推荐阅读
  

闽ICP备14008679号