Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;从fcntl.h读取函数中的字符串_C++_Unix_Fcntl_Unistd.h - Fatal编程技术网

C++ C++;从fcntl.h读取函数中的字符串

C++ C++;从fcntl.h读取函数中的字符串,c++,unix,fcntl,unistd.h,C++,Unix,Fcntl,Unistd.h,在我大学的Linux编程基础课程中,我们使用fcntl.h和unistd.h 使用C++字符串,我得到以下: statusOfFunction = write(fileDescriptor, input.c_str(), input.length()); 这条线行得通。我创建了一个文件,其中包含输入字符串的内容。但是,为什么这些行都不起作用: statusOfFunction = read(fileDescriptor, reading.c_str(), 10); Error: No matc

在我大学的Linux编程基础课程中,我们使用fcntl.h和unistd.h 使用C++字符串,我得到以下:

statusOfFunction = write(fileDescriptor, input.c_str(), input.length());
这条线行得通。我创建了一个文件,其中包含输入字符串的内容。但是,为什么这些行都不起作用:

statusOfFunction = read(fileDescriptor, reading.c_str(), 10);
Error: No matching function call to "read"

statusOfFunction = read(fileDescriptor, reading, 10);
Error: No matching function call to "read"

statusOfFunction = read(fileDescriptor, &reading, 10);
No error throws up, but does not get executed

statusOfFunction = read(fileDescriptor, &reading.c_str(), 10);
Error: No matching function call to "read"

这是程序,供你参考。
谢谢!:)

第一个问题

statusOfFunction = read(fileDescriptor, reading.c_str(), 10);
声明
c_str
返回一个
const
指针。否则,这就是接近正确的方法

首先,您需要创建一个至少包含10个字符的字符串:

std::string temp_string(10, ' ');  // Creates a string contains 10 spaces
然后需要传递一个非常量指针,可以使用操作符
&
的地址和字符串数组索引操作符
[]

statusOfFunction = read(fileDescriptor, &temp_string[0], temp_string.length());
最后,将其指定给实际字符串:

reading = std::string(temp_string, 0, statusOfFunction);
当然,您应该检查
statusOfFunction
,这样它就不是错误(当它
-1
时)或文件结束(当它
0
时)



您尝试的所有其他读取方法都是非常错误的。

read需要一个缓冲区来填充读取的数据

你所做的是危险的

您应该分配一个字符缓冲区,并确保在字符串长度后添加空字符,因为
read
不会为您这样做。 c_str函数公开string类使用的内部缓冲区。它是只读的

在以后使用时,覆盖字符串对象本身肯定会导致崩溃

char buff[11];
size_t bytes_read = read(fd, buff, 10);
buff[bytes_read] = 0; // terminate string
string myString(buff);

正在读什么?您需要将原型与
read
匹配,根据
man2 read
的规定,原型是
ssize\t read(int-fd,void*buf,size\t count)
所以你需要读取一个空指针,指向一个足够容纳10个字节的缓冲区(或者不管你的
计数是多少)。在你的基本Linux编程课程中,他们还没有涵盖
const
的含义吗?它仍然不起作用:(请检查代码。我哪里都错了吗?@NadeemAhmedNajeeb对于初学者,您错过了我告诉您创建字符串并为其分配内存的那一位。您在链接代码中所做的操作被读入一个空字符串,导致未定义的行为。我确实尝试过,但我也得到了一个错误:std::string sample(10);初始化时也没有匹配的构造函数,我尝试使用示例[10],思考[]可能超负荷了,结果我错了too@NadeemAhmedNajeeb很抱歉,我忘记了构造函数需要额外的参数,更新了答案。这里的主要问题是,您使用的是
std::string
,它并不是真正为之设计的。和
运算符[]
在类中重载,请阅读链接引用。Joachim,非常感谢!:)我从代码中漏掉的两件事,你们都纠正了:1漏掉了:lseek(fileDescriptor,0,SEEK_SET);2构造函数中漏掉的参数谢谢你的回答:)所以,在UNIX库中使用C++字符串不是很大的选择,不推荐使用它们。I/O有C++类,混合低级别C函数,字符串类是危险的。无论如何,<代码> CyScript()/Cyto>返回一个const数组,因此您不应该修改它。C++ I/O也可以用于UNIX吗?C++ + IO类在Linux和unix .WOW上运行得很好。在印度,就Unix编程而言,唯一的答案是C。猜一猜冒险会有帮助,谢谢!☺️