簡體   English   中英

從二進制文件讀取並存儲到緩沖區

[英]read from binary file and store to a buffer

有人可以告訴我這是否正確嗎? 我嘗試逐行讀取二進制文件並將其存儲在緩沖區中嗎? 它存儲在緩沖區中的新行會刪除先前存儲的行嗎?

        ifs.open(filename, std::ios::binary);
        for (std::string line; getline(ifs, line,' '); )
                {
                    ifs.read(reinterpret_cast<char *> (buffer), 3*h*w);

                }

由於某種原因,您混用了基於文本的讀取getline和作為二進制讀取的read()

另外,還不清楚,什么是buffer ,什么大小。 因此,這是一個簡單的示例供您開始:

ifs.open(filename, std::ios::binary); // assume, that everything is OK

constexpr size_t bufSize = 256;
char buffer[bufSize];
size_t charsRead{ 0 };
do {
    charsRead = ifs.read(buffer, bufSize)
    // check if charsRead == 0, if it's ok
    // do something with filled buffer.
    // Note, that last read will have less than bufSize characters,
    // So, query charsRead each time.
} while (charsRead == bufSize);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM