C++ Linux API和C++;性病媒介
在使用linux api(读、写)与文件系统交互时,使用矢量(矢量无符号字符)而不是字符数组(字符[])有多安全和正确?需要建设性的批评。还有其他选择吗 我想在编写包装器库(使用类)时使用这种技术 代码示例:C++ Linux API和C++;性病媒介,c++,linux,stdvector,C++,Linux,Stdvector,在使用linux api(读、写)与文件系统交互时,使用矢量(矢量无符号字符)而不是字符数组(字符[])有多安全和正确?需要建设性的批评。还有其他选择吗 我想在编写包装器库(使用类)时使用这种技术 代码示例: // This program was written to test the possibility of using a vector as a buffer // for reading and writing to a file using linux api. #include
// This program was written to test the possibility of using a vector as a buffer
// for reading and writing to a file using linux api.
#include <iostream>
#include <vector>
#include <fcntl.h> // Linux API open
#include <unistd.h> // Linux API read,write,close
using namespace std;
int main() {
vector <unsigned char> buffer(10);
buffer[0] = '1';
buffer[1] = '2';
buffer[2] = '3';
buffer[3] = '4';
buffer[4] = '5';
buffer[5] = '5';
buffer[6] = '4';
buffer[7] = '3';
buffer[8] = '2';
buffer[9] = '1';
// Open new file for writing (create file)
int fd = 0;
const char *path = "./test.txt";
fd = (open(path, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU));
if (fd == -1) {
cout << "Can't open (create) file!!!" << endl;
return 0;
}
// Write to file from vector
write(fd, &buffer, buffer.size());
// Close file
close(fd);
// Open file for reading
fd = open(path, O_RDONLY);
vector <unsigned char> buffer1(10);
// Read from file to vector
read (fd,&buffer1,buffer1.size());
// close file
close(fd);
return 0;
}
//编写此程序是为了测试使用向量作为缓冲区的可能性
//用于使用linux api读取和写入文件。
#包括
#包括
#include//Linux-API-open
#包括//Linux API读、写、关闭
使用名称空间std;
int main(){
向量缓冲器(10);
缓冲区[0]=“1”;
缓冲区[1]=“2”;
缓冲区[2]=“3”;
缓冲区[3]=“4”;
缓冲区[4]=“5”;
缓冲区[5]=“5”;
缓冲区[6]=“4”;
缓冲区[7]=“3”;
缓冲区[8]=“2”;
缓冲区[9]=“1”;
//打开新文件进行写入(创建文件)
int-fd=0;
const char*path=“./test.txt”;
fd=(开放(路径,O|u WRONLY | O|u CREAT | O|u TRUNC,S|u IRWXU));
如果(fd==-1){
我不能回答你的疑问和问题
在使用linux api(读、写)与文件系统交互时,使用矢量(矢量无符号字符)而不是字符数组(字符[])有多安全和正确
将std::vector
、std::array
、std::string
与期望std::string
的unsigned char[]
或char[]
的连续数组(在std::string
的情况下以null结尾)的API函数一起使用是完全安全的
上面提到的所有这些类都提供了一个data()
成员,允许直接访问底层数据
还有其他选择吗
是的,还有其他方法可以更手动地管理这些指针,例如std::unique_ptr
或std::shared_ptr
,但通常您不需要它们
我想在编写包装器库(使用类)时使用这种技术
这是创建此类API包装器类的常见、灵活和健壮的技术
关于示例代码
及
是错误的,因为&buffer
(1
)没有指向由std::vector
类管理的基础数据的地址
您需要使用函数访问指向基础数据的指针:
// Write to file from vector
write(fd, buffer.data(), buffer.size());
// Read from file to vector
read (fd,buffer1.data(),buffer1.size());
需要建设性的批评。还有其他选择吗
在您的特定情况下,您似乎主要希望从底层文件写入和读取文本。在这种情况下,我更喜欢使用std::string
,而不是std::vector
这使得写像这样的东西更容易
std::string buffer = "123455432";
用于数据初始化。write(fd,&buffer,buffer.size());
错误。应使用write(fd,buffer.data(),buffer.size())
相反。同样适用于std::string也适用于std::string
顺便说一句。谢谢。我做了一些修改。效果非常好。文件已经变得可读性很好。有什么方法可以在声明时初始化无符号字符向量吗?有选择,可以在声明时初始化向量。您打算如何初始化它类似这样的东西:向量缓冲区(10)={1,2,3,4,5,6,7,8,9,10};。据我所知(我是C++初学者)最近,建议使用位集容器来执行位操作和低级编程。这是真的吗?在这方面,您还有一个问题。位集容器是否可以与Linux API的读写函数一起使用?@BladeMon如果您还有其他问题,请写一个新的问题但是关于std::bitset
的使用,你可能误解了一些东西,或者有人在对你胡说八道。只要你不需要或不想处理任何单个位,你就不需要std::bitset
,一个连续字节的缓冲区是完全可以的,而低级别读取()
和write()
操作。若要处理读取内容中的单个位序列,您可以随后使用缓冲区中的单个或一些字节创建std::bitset
。再次感谢。我将创建一个新问题。
// Write to file from vector
write(fd, buffer.data(), buffer.size());
// Read from file to vector
read (fd,buffer1.data(),buffer1.size());
std::string buffer = "123455432";