unistd read() 不起作用
unistd read() doesn't work
我正在尝试使用 unistd 库写入/读取文本文件。稍后我会将其部署到/dev 路径中的文件中。 我正在尝试使用std::vector
类型而不是char buffer[]
.所以我写了两个实现,一个使用向量,另一个使用char缓冲区。两者都在文件中写入良好,但都无法读取文件。
这是我的测试代码:
使用字符缓冲区
void tests_using_buffer_of_char(const std::string &path)
{
int file_descriptor_char;
char buffer[8] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
std::cout << sizeof(buffer) << std::endl;
for(auto i = 0; i < sizeof(buffer); i++)
std::cout << buffer[i] << std::endl;
file_descriptor_char = open(path.c_str(), O_RDWR);
if(file_descriptor_char != -1)
std::cout << "open with success " << path << std::endl;
std::size_t size = strlen(buffer);
ssize_t write_bytes = write(file_descriptor_char, buffer, size);
std::cout << size << " <-- size of buffer " << write_bytes << " <-- size of writed bytes" << std::endl;
memset(&buffer[0], 0, sizeof(buffer));
ssize_t read_bytes = read(file_descriptor_char, &buffer, size);
std::cout << size << " <-- size of buffer " << read_bytes << " <-- size of readed bytes" << std::endl;
for(auto i = 0; i < sizeof(buffer); i++)
std::cout << buffer[i] << std::endl;
if(close(file_descriptor_char) != -1)
std::cout << "close with success "<< path << std::endl;
}
使用矢量缓冲区
void tests_using_buffer_of_vector(const std::string &path)
{
int file_descriptor_vector;
std::vector<char> buffer = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
std::cout << buffer.size() << std::endl;
for(auto i : buffer)
std::cout << i << std::endl;
file_descriptor_vector = open(path.c_str(), O_RDWR);
if(file_descriptor_vector != -1)
std::cout << "open whit success " << path << std::endl;
ssize_t write_bytes = write(file_descriptor_vector, buffer.data(), buffer.size());
std::cout << buffer.size() << " <-- size of buffer " << write_bytes << " <-- size of writed bytes" << std::endl;
buffer.clear();
ssize_t read_bytes = read(file_descriptor_vector, buffer.data(), write_bytes);
std::cout << buffer.size() << " <-- size of buffer " << read_bytes << " <-- size of readed bytes" << std::endl;
for(auto i : buffer)
std::cout << i << std::endl;
if(close(file_descriptor_vector) != -1)
std::cout << "close with success " << path << std::endl;
}
谢谢大家!
1] 只需使用fclose
并重新打开文件即可。
2]fseek(fp, 0, SEEK_SET);
3]rewind(fp);
您指向带有file pointer
的文件末尾。
您必须将文件描述符重置为文件的开头,然后在使用它进行写入后尝试重用它进行读取。您可以关闭文件并重新打开它,也可以寻找开头。
lseek(file_descriptor_vector, SEEK_SET, 0);
在读取向量之前,不要清除它。clear
方法实质上将释放与向量关联的内存。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- unistd read() 不起作用
- c++fstream read()函数不起作用