ppm file view

Can i view a ppm file in format to check that the values that
i store in the buffer are the same?
The question isn't clear.
Are you reading an existing file, or creating a new file?
Which buffer? Check which values are the same as which other values?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using byte = unsigned char ;

std::vector<byte> get_bytes( const std::string& file_name )
{
    std::ifstream file( file_name, std::ios::binary ) ; // open for input in binary mode
    file >> std::noskipws ; // we want to read every byte, including white space

    std::istream_iterator<byte> begin(file), end ;
    return { begin, end } ;
}

// verify that the buffer contains the bytes in the file
bool verify_contents( const std::vector<byte>& buffer, const std::string& file_name )
{
    std::ifstream file( file_name, std::ios::binary ) ; // open for input in binary mode
    file >> std::noskipws ; // we want to read every byte, including white space

    std::istream_iterator<byte> begin_file(file), end_file ;

    #ifdef NDEBUG
\
        return std::equal( std::begin(buffer), std::end(buffer), begin_file, end_file )

    #else

        const auto pair = std::mismatch( std::begin(buffer), std::end(buffer), begin_file, end_file ) ;
        const bool matched = pair.first == std::end(buffer) && pair.second == end_file ;
        
        if(matched) std::cout << "matched!\n" ;
        else std::cout << "mismatch at byte #" << pair.first - std::begin(buffer) << '\n' ;
        
        return matched ;

    #endif // NDEBUG
}

int main()
{
    const std::string file_name = __FILE__ ; // this file
    auto buffer = get_bytes(file_name) ;


    verify_contents( buffer, file_name ) ; // matched

    if( buffer.size() > 123 ) ++buffer[123] ;
    verify_contents( buffer, file_name ) ; // mismatch at byte #123
}

http://coliru.stacked-crooked.com/a/5112c54d0f3cca5c
http://rextester.com/DQBWF57178
Topic archived. No new replies allowed.