简体   繁体   English

unistd read()不起作用

[英]unistd read() doesn't work

I am trying to write / read text files using the unistd library. 我正在尝试使用unistd库编写/读取文本文件。 I will later deploy it to a file in the / dev path. 稍后,我将其部署到/ dev路径中的文件中。 I'm trying to use std::vector type instead of char buffer[] . 我正在尝试使用std::vector类型代替char buffer[] So I wrote two implementations, one using vector and another char buffer. 所以我写了两个实现,一个使用向量,另一个使用char缓冲区。 Both write well in the file, but neither can read the file. 两者都可以很好地在文件中写入,但是都不能读取文件。

Here is my test code: 这是我的测试代码:

using buffer of char 使用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;
}

using buffer of vector 使用向量缓冲区

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;

}

Thanks everyone! 感谢大家!

You have to reset your file descriptor to be at the beginning of the file before trying to reuse it for reading after you used it for writing. 您必须将文件描述符重置为文件的开头,然后再使用它进行写入后再尝试将其用于读取。 You could close the file and reopen it, or you can seek to the beginning. 您可以关闭文件然后重新打开它,也可以从头开始。

lseek(file_descriptor_vector, SEEK_SET, 0);

Don't clear your vector before reading into it. 在读入向量之前,请不要清除它。 the clear method will essentially deallocate the memory associated with the vector. clear方法将实质上释放与向量关联的内存。

1] Just use fclose and reopen the file. 1]只需使用fclose并重新打开文件即可。

2] fseek(fp, 0, SEEK_SET); 2] fseek(fp, 0, SEEK_SET);

3] rewind(fp); 3] rewind(fp);

You are pointing to the end of the file with file pointer . 您使用file pointer文件的末尾。

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

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