资讯

精准传达 • 有效沟通

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

c语言用read函数通信 read c语言

C中read()的用法?

read()函数的原型是int read(int fd,void *buf,int count);。它的功能是“从文件说明符fd相关联的文件中读取count个字符,并把这些字符存储到buf所指的缓冲区中。返回值是操作成功时所读到的字节数,在文件结束时可能少于count个字节;若返回值为-1则说明出错了,返回0则表示到达文件尾端。例:从文件ABC.txt中读取前100个字节存入数组buffer中——

成都创新互联为企业级客户提高一站式互联网+设计服务,主要包括做网站、成都网站建设、成都App制作、微信小程序、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。 

#include "stdin.h"

#include "io.h"

#include "fcnt1.h"

int main(void){

int fd;

char buffer[100];

if((fd=open("ABC.txt",O_RDONLY))==-1){

printf("Can't open file.\n");

exit(-1);

}

if(read(fd,buffer,100)!=100)

printf("Possible read error!\n");

}

c语言socket编程read()函数问题

write/read,是关于写特定的文件描述符使用的比较底层的系统调用,

sendto/recvfrom适用于用udp(数据包)方式的套接字中使用的,

其他的没见过,我是学linux编程的

Windows下的socket和Linux下的socket是不一样的,我没有查到sendmsg/recvmsg这个方法,上面的链接是一个类似的函数,它和send/recv 的区别是,这个函数可以发送socket控制信息,看API就知道。

另外我也没有看到有write/read函数。 sendto/recvfrom用于UDP链接,send/recv用于TCP链接。

你列举的其余函数,我都没找到。下面的API地址供楼主查询验证。

arm6410,linux,c语言,read函数返回-1,错误:bad address

1、可以事先检查一下传递给 read() 函数的 fd 是否合法,即在 'if ((nread = read(fd,myBuff2,strlen(myBuff2)))0)' 之前判断 if ( fd == NULL ) printf("出错啦!\n");

2、read()函数是文件操作函数,在c语言中很重要。

函数的返回值如下:

(1)如果成功,返回读取的字节数;

(2)如果出错,返回-1并设置errno;

(3)如果在调read函数之前已是文件末尾,则返回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

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语言 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函数

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函数通信 read c语言
网站网址:http://cdkjz.cn/article/hpscho.html
多年建站经验

多一份参考,总有益处

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

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

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220