Reading and writing binary file

前端 未结 7 664
挽巷
挽巷 2020-11-22 16:29

I\'m trying to write code to read a binary file into a buffer, then write the buffer to another file. I have the following code, but the buffer only stores a couple of ASCI

7条回答
  •  难免孤独
    2020-11-22 16:47

    If you want to do this the C++ way, do it like this:

    #include 
    #include 
    #include 
    
    int main()
    {
        std::ifstream input( "C:\\Final.gif", std::ios::binary );
        std::ofstream output( "C:\\myfile.gif", std::ios::binary );
    
        std::copy( 
            std::istreambuf_iterator(input), 
            std::istreambuf_iterator( ),
            std::ostreambuf_iterator(output));
    }
    

    If you need that data in a buffer to modify it or something, do this:

    #include 
    #include 
    #include 
    
    int main()
    {
        std::ifstream input( "C:\\Final.gif", std::ios::binary );
    
        // copies all data into buffer
        std::vector buffer(std::istreambuf_iterator(input), {});
    
    }
    

提交回复
热议问题