how create and read, correctly IStream from file?

i'm overloading input and output file operators:
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
friend std::ostream& operator << (std::ostream& lhs, const image& rhs)
    {

        //Create an empty IStream:
        IStream* pIStream;
        if(CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)pIStream)!=S_OK)
            DebugText("error on creating an empty IStream");

        //choose image format for save it on IStream:
        // Get encoder class id for jpg compression
        // for other compressions use
        //    image/bmp
        //    image/jpeg
        //    image/gif
        //    image/tiff
        //    image/png
        CLSID pngClsid;
        GetEncoderClsid(L"image/gif", &pngClsid);

        // Setup encoder parameters
        EncoderParameters encoderParameters;
        encoderParameters.Count = 1;
        encoderParameters.Parameter[0].Guid = EncoderQuality;
        encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
        encoderParameters.Parameter[0].NumberOfValues = 1;

        // setup compression level
        ULONG quality = 50;
        encoderParameters.Parameter[0].Value = &quality;

        //  Save the image to the stream
        if(rhs.img->Save(pIStream, &pngClsid, &encoderParameters) != Ok)
        {
            pIStream->Release();
            DebugText("error on saving to IStream");
        }

        //getting the stream size:

        STATSTG sts;
        pIStream->Stat(&sts, STATFLAG_DEFAULT);
        ULARGE_INTEGER uli = sts.cbSize;
        LARGE_INTEGER zero;
        zero.QuadPart = 0;
        ULONG size = (ULONG)uli.QuadPart;
        char* bits = new char[size];
        ULONG written;
        pIStream->Seek(zero, STREAM_SEEK_SET, NULL);
        pIStream->Read(bits, size, &written);

        //write the stream size on file
        lhs.write(reinterpret_cast<char*>(&size),sizeof(size));

        //write pBuff data on file
        lhs.write(reinterpret_cast<char*>(&bits),size);
        //clean resources
        delete[] bits;
        pIStream->Release();

        return lhs;
    }

    friend std::istream& operator >> (std::istream& lhs, image& rhs)
    {
        //getting IStream size:
        int streamsize;
        lhs.read(reinterpret_cast<char*>(&streamsize), sizeof(streamsize));

        // getting IStream data:
        IStream* pIStream;
        HGLOBAL hg= ::GlobalAlloc(GMEM_MOVEABLE,streamsize);
        void* p =GlobalLock(hg);
        lhs.read(reinterpret_cast<char*>(p),streamsize);
        GlobalUnlock(hg);

        if(CreateStreamOnHGlobal(hg, TRUE, (LPSTREAM*)pIStream)!=S_OK)
            DebugText("error on creating an empty IStream");


        LARGE_INTEGER zero;
        zero.QuadPart = 0;
        pIStream->Seek(zero, STREAM_SEEK_SET, NULL);
        //reading IStream to Image class:
        rhs.img=Image::FromStream(pIStream);
        Status stat = rhs.img->GetLastStatus();
        if(stat!=S_OK)
            DebugText("error reading stream to Image: "  + to_string(stat));
        else
        {
            DebugText("reading stream to Image sucessfull");
        }


        //realease resources:
        pIStream->Release();
        return lhs;
    }

by some reason these code have 1 error that the compiler can't detect.
when i end the program, the program stills executed.
can anyone explain it?
i found 1 error: when we use GlobalAlloc(), we must use GlobalFree(). maybe i have more errors that i can't find them
Why do you think that those stream operators are the culprits?
maybe you have right.
but i'm getting problems here:
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
struct user
{
    string name;
    int age;
    image foto;
};

//save a structure object to a file
template<typename structure>
void SaveDataBase(string filename,structure *StructureVariable )
{
    remove(filename.c_str());
    ofstream output_file(filename.c_str(), ios::binary);
    //output_file.write(reinterpret_cast<char*>(StructureVariable), sizeof(structure));
    output_file<<StructureVariable;
    output_file.close();
}

//read a structure object from a file
template<typename structure>
void ReadDataBase(string filename,structure *StructureVariable )
{
    ifstream input_file(filename.c_str(), ios::binary);
    input_file>>StructureVariable;
    input_file.close();
}

on:
input_file>>StructureVariable;
error:
"cannot bind 'std::basic_istream<char>' lvalue to 'std::basic_istream<char>&&'"
so what i'm doing wrong?
There are actually some problem in your streaming operators in your original post:

Line 52: The type is ULONG. Line 67: The type is int. This is not the same and causes problem especially if the size of the types are different. Better use the same type for reading and writing.

Line 55: You write the pointer to the pointer. This results in garbage data. Remove the & before bits.

so what i'm doing wrong?
StructureVariable is a pointer and the stream operators probably don't take a pointer. At least not the operators in your op.
Topic archived. No new replies allowed.