简体   繁体   English

来自fcntl.h的读取函数中的C ++字符串

[英]C++ Strings in Read Function from fcntl.h

In my basic Linux Programming course at college, we use fcntl.h and unistd.h Using C++ strings, I get the following: 在我大学的基础Linux编程课程中,我们使用fcntl.h和unistd.h使用C ++字符串,得到以下信息:

statusOfFunction = write(fileDescriptor, input.c_str(), input.length());

This line works. 这条线有效。 I get a file created, with the contents of input string. 我创建了一个包含输入字符串内容的文件。 But, why doesn't any of these lines work: 但是,为什么这些行都不起作用:

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"

https://www.dropbox.com/s/lnw208uo3xurqxf/Basic%20Unix%20Operations%20on%20Text%20Files.cpp?dl=0 https://www.dropbox.com/s/lnw208uo3xurqxf/Basic%20Unix%20Operations%20on%20Text%20Files.cpp?dl=0

Here is the program, for your reference. 这是程序,供您参考。 Thank you! 谢谢! :) :)

The problem with the first 第一个问题

statusOfFunction = read(fileDescriptor, reading.c_str(), 10);

is that c_str is declared to return a const pointer. c_str被声明为返回const指针。 Otherwise this is the closes to the correct method. 否则,这接近正确的方法。

First of all you need to create a string that contains at least 10 characters: 首先,您需要创建一个至少包含10个字符的字符串:

std::string temp_string(10, ' ');  // Creates a string contains 10 spaces

Then you need to pass a non-constant pointer, which you can get by using the address-of operator & and the strings array-indexing operator [] : 然后,您需要传递一个非常数指针,可以通过使用address-of运算符&和字符串array-indexing运算符[]获得该常数:

statusOfFunction = read(fileDescriptor, &temp_string[0], temp_string.length());

And finally you assign it to the actual string: 最后,将其分配给实际的字符串:

reading = std::string(temp_string, 0, statusOfFunction);

Of course, you should check statusOfFunction so it's not an error (when it -1 ) or end of file (when its 0 ). 当然,您应该检查statusOfFunction这样就不会出现错误(当它为-1 )或文件结尾(当它为0 )。


All the other methods you tried for reading are just very wrong. 您尝试阅读的所有其他方法都非常错误。

read expects a buffer to fill the data read. read需要一个缓冲区来填充读取的数据。

What you're doing is dangerous. 你在做什么很危险。

You should allocate a char buffer and make sure you add the NULL char after the string length as read will not do it for you. 您应该分配一个char缓冲区,并确保在字符串长度后添加NULL char,因为read不会为您这样做。 the c_str function exposes the internal buffer used by the string class. c_str函数公开了字符串类使用的内部缓冲区。 It is read only. 它是只读的。

Overwriting the string object itself will cause a crash for sure when later used. 覆盖字符串对象本身肯定会在以后使用时导致崩溃。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM