189 8069 5689

c语言read函数格式 c语言read函数读到什么结束

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语言中的read和write怎么用?

1.纠正:read和write是UNIX或者一些类UNIX系统,比如LINUX系统中使用的,称为LINUX系统函数。这种函数只能在特定的操作系统下使用,可移植性差。fread和fwrite是C库函数。这种函数基本在任何操作系统都能使用,可移植性高。2.基础知识介绍只介绍LINUX系统函数,常用的有creat,open,close,read,write,lseek,access,一般用于文件编程3.如何使用谈到如何使用就必须说到另一个知识,文件描述符(file description),是一个非负数。函数原型:int read(int fd, const void *buf, size_t length)功能: 从文件描述符fd所指向的文件中读取length个字节到buf所指向的缓存区中,返回值为实际读取的字节数int write(int fd, const void *buf, size_t length)功能: 把length个字节从buf所指向的缓存区中写到件描述符fd所指向的文件中,返回值为实际写入的字节数 例子:#define LENGTH 1024#define BUFFES_SIZE 1024int n1, n2;int fd1, fd2;int buffer[BUFFES_SIZE];fd1 = open( "HEllo1.txt", O_RDWR | O_CREAT, O_IRUSE | O_IWUSR);fd2 = open( "HEllo2.txt", O_RDWR | O_CREAT, O_IRUSE | O_IWUSR);n1 = read( fd1, buffer, LENGTH);n2 = write( fd2, buffer, n1); 好了累死了,答案完全原创,希望对你有帮助

C语言read函数

read内部是调_read, _read的返回值在msdn中有这样的描述

_read returns the number of bytes read, which might be less than count if there are fewer than count bytes left in the file or if the file was opened in text mode, in which case each carriage return–line feed (CR-LF) pair is replaced with a single linefeed character. Only the single linefeed character is counted in the return value. The replacement does not affect the file pointer.

注意这一段: in which case each carriage return–line feed (CR-LF) pair is replaced with a single linefeed character

就是说如果用text模式打开的话, 文件换行时可能在文本中有2个字符----换行和缩进(CR-LF), 而在return的时候系统是把它作为1个回车符号('\n')所返回的. 所以会导致这个情况


网页名称:c语言read函数格式 c语言read函数读到什么结束
当前链接:http://cdxtjz.cn/article/dopccep.html

其他资讯