资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言read函数的作用 c++中read函数的使用

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

1024

int

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语言 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()函数,谢谢

有区别的

if(fd=open("tem.txt",O_RDWR)==-1)

这里的话是先运行open("tem.txt",O_RDWR)==-1这个的,这个的值是0或者1的‘

那么FD的值就不是文件的头指针了

而下面的是先运行fd=open("tem.txt",O_RDWR

然后再将FD和-1作比较的


当前标题:c语言read函数的作用 c++中read函数的使用
本文链接:http://cdkjz.cn/article/ddddgic.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220