Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Напиши функцию для построчного чтения бинарного файла на языке cpp

  1. User

    Напиши функцию для построчного чтения бинарного файла на языке cpp

  2. Assistant

    Вот пример функции для построчного чтения бинарного файла на языке C++:

    #include <iostream>
    #include <fstream>
    #include <vector>
    
    std::vector<std::string> ReadFileLines(const std::string& filename) {
        std::ifstream file(filename, std::ios::binary);
        std::vector<std::string> lines;
        std::string line;
    
        if (file.is_open()) {
            while (std::getline(file, line)) {
                lines.push_back(line);
            }
            
            file.close();
        } else {
            std::cout << "Unable to open file: " << filename << std::endl;
        }
    
        return lines;
    }
    

    Вы можете использовать эту функцию, чтобы прочитать бинарный файл, например, так:

    #include <iostream>
    #include <string>
    #include <vector>
    
    int main() {
        std::string filename = "example.bin";
        std::vector<std::string> lines = ReadFileLines(filename);
        
        for (const std::string& line : lines) {
            std::cout << line << std::endl;
        }
        
        return 0;
    }
    

    В данном примере прочитанные строки будут напечатаны на консоль.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.