定义一个c++public类,实现Rs485openreadwriteclose等函数功能 - 爱问答

(爱问答)

定义一个c++public类,实现Rs485openreadwriteclose等函数功能

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、程序示例:

123456789101112131415161718192021222324#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <sysstat.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. ");exit(1);}strcpy(string, "Hello, world! ");length = strlen(string);if ((res = write(handle, string, length)) != length){printf("Error writing to the file. ");exit(1);}printf("Wrote %d bytes to the file. ", res);close(handle); return 0; }


下一篇:怎样能准确获取无人机的实时位置和姿态呢?

上一篇:从键盘输入两个字符串,对其进行比较,然后输出两个字符串中第一个不相同字符的ASCII码之差。

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?