Image rd file compress

Pages: 1234
To open file in binary read mode in c++, can i use this way?

1
2
3
4
5
6
7
8
9
10
11
12
FILE *infile;
infile = fopen(filename, "rb");
std::cout << "File got opened"<< std::endl;
if (!infile)
return false;
if (fread(&imghdr, sizeof(imghdr), 1, file) != 1 || fread(&ImginfoHdr, sizeof(ImginfoHdr), 1, file) != 1 )
{
fclose(infile);
return 0;
}





Below is my modified code, but i get the following error.
[Error] no matching function for call to 'std::basic_ifstream<char>::read(std::ifstream&)'

[Note] std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::read(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long long int]

[Note] candidate expects 2 arguments, 1 provided

1
2
3
4
5
6
7
8
9
10
11
12

ifstream infile;
file.open (filename, std::ios::binary | std::ios::in); 
std::cout << "File got opened"<< std::endl;
if (!infile)
return false;
if (infile.read((&imghdr, sizeof(imghdr), 1, file)) != 1 || infile.read((&ImginfoHdr, sizeof(ImginfoHdr), 1, file)) != 1 )
{
infile.close();
return 0;
}


[/code]
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if( std::ifstream infile{ filename, std::ios::binary } )
{
    // https://en.cppreference.com/w/cpp/io/basic_istream/read

    // sanity check:
    // TriviallyCopyable - raw bytes in the object representation
    //                     can be safely copied and can be read from binary files with read()
    // https://en.cppreference.com/w/cpp/types/is_trivially_copyable
    static_assert( std::is_trivially_copyable< decltype(imghdr) >::value, "not a trivially copyable type" ) ;
    static_assert( std::is_trivially_copyable< decltype(ImginfoHdr) >::value, "not a trivially copyable type" ) ;

    if( infile.read( reinterpret_cast<char*>( std::addressof(imghdr) ), sizeof(imghdr) ) &&
        infile.read( reinterpret_cast<char*>( std::addressof(ImginfoHdr) ), sizeof(ImginfoHdr) ) )
                return true ; // success for both reads
}

return false ; // file not opened or a read failed 
Thanks a lot JLBorges. While I add the above code, it gives me below errors.

[Error] 'is_trivially_copyable' is not a member of 'std'
[Error] expected primary-expression before 'decltype'
[Error] expected ',' before 'decltype'
[Error] expected string-literal before 'decltype'
[Error] expected ')' before 'decltype'

My GCC version is < 5 do not support std::is_trivially_copyable from the C++11 standard. are there any other work arounds?

Is write also done with the same logic as above?

Below is my C code.

1
2
3
4
    fwrite(&imghdr, sizeof(imghdr), 1, infile);
    fwrite(&imginfoHdr, sizeof(imginfoHdr), 1, infile);
    fwrite(&img.pix[0], imginfoHdr.imgsize, 1, file);

Last edited on
What do you actually want?
C or C++ ?
It's not a good idea to mix two different languages.
> Is write also written the same way as above?

Yes; except that it is called on an output stream and that a cast to const char* would suffice.
https://en.cppreference.com/w/cpp/io/basic_ostream/write
> My GCC version is < 5 do not support std::is_trivially_copyable from the C++11 standard.
> are there any other work arounds?

It is an ancient version of the compiler.
Just comment out the lines with static_assert .
Verify manually that the types involved are trivially copyable.
Verify manually means?

Below is my code, so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

ifstream infile;
file.open (filename, std::ios::binary | std::ios::in); 
std::cout << "File got opened"<< std::endl;
if (!infile)
return false;
if (infile.read((&imghdr, sizeof(imghdr), 1, file)) != 1 || infile.read((&imginfoHdr, sizeof(imginfoHdr), 1, file)) != 1 )
{
infile.close();
return 0;
}

fseek(infile, imghdr.setbits, SEEK_SET);
    if (fread(&img.pix[0], img.pix.size(), 1, infile) != 1)
    {
        fclose(infile);
        return false;
    }



Also what is the equivalent for the fwrite in C++?
I need code to be in C++.

1
2
3
4
    fwrite(&imghdr, sizeof(imghdr), 1, infile);
    fwrite(&imginfoHdr, sizeof(imginfoHdr), 1, infile);
    fwrite(&img.pix[0], imginfoHdr.imgsize, 1, file);

Last edited on
> Verify manually means?

Somewhat simplified;
verify that the types involved (types of imghdr, imginfoHdr etc.) are plain C-style structs (structs that a C program would not complain about), and that they do not have pointers as members.


> what is the equivalent for the fwrite in C++?

https://en.cppreference.com/w/cpp/io/basic_ostream/write
Topic archived. No new replies allowed.
Pages: 1234