0

The script used by me to write a vector of structure cameras to a binary file:

ofstream file;
file.open(cameraParamFile.c_str(), ios_base::binary);
const char* pointer = reinterpret_cast<const char*>(&cameras[0]);
size_t bytes = cameras.size() * sizeof(cameras[0]);
file.write(pointer, bytes);

This is what the structure looks like:

struct CV_EXPORTS CameraParams
{
    CameraParams();
    CameraParams(const CameraParams& other);
    const CameraParams& operator =(const CameraParams& other);
    Mat K() const;

    double focal; // Focal length
    double aspect; // Aspect ratio
    double ppx; // Principal point X
    double ppy; // Principal point Y
    Mat R; // Rotation
    Mat t; // Translation
};

and here is the vector declaration:

vector<CameraParams> camerasTest

Now when I try reading from this file, I am unable to do it (still a newbie to c++). It gives me error saying no matching function for call to 'std::basic_ifstream::read(std::vector, size_t&)'|*.

Here is my attempt:

vector<CameraParams> camerasTest;
ifstream inf1;
inf1.open(cameraParamFile.c_str(), ios::binary | ios::in);
inf1.read(&camerasTest, bytes));

I think this is an issue related to typecasting, or incorrect arguments. Can someone please point out the right way?

I tried reading the below mentioned link but couldn't make much sense: Write and load vector of structs in a binary file c++

EDIT

As suggested in the comments I tried implementing custom reader and writer using the above link. Here are function written by me. It still does not work.

void writeStruct(ostream& out, const vector<CameraParams> &vec)
{ 
    typename vector<CameraParams>::size_type size = vec.size();
    os.write((char*)&size, sizeof(size));
    os.write((char*)&vec[0], vec.size() * sizeof(CameraParams));
}

void readStruct(istream& is, vector<CameraParams> &vec)
{
    typename vector<CameraParams>::size_type size = 0;
    is.read((char*)&size, sizeof(size));
    vec.resize(size);
    is.read((char*)&vec[0], vec.size() * sizeof(CameraParams));
}

When I try to print the values using the above reading function using the below script:

vector<CameraParams> cameraTest;
std::ifstream in(cameraParamFile.c_str(), std::ios::in | std::ios::binary);
readCameraParamsVector(in, cameraTest);
in.close();

cout << cameraTest[0].R;
cout << "\n";
cout << cameras[0].R;

It starts giving me a run time error saying process terminated with status -1073741819

EDIT 2 As mentioned in the comments why I couldn't use the answer mentioned in the link Write and load vector of structs in a binary file c++ ? I have already mentioned in EDIT 1 that I wrote a write and read function using that link but still gives me the error as shown above.

Community
  • 1
  • 1
raj
  • 57
  • 11
  • The error is very clear, `read` doesnt take a `vector*`, look at the code you have to writing and spot the difference (also that vector doesn't have any space before the read operation so you're going to need to resize ahead of time or repeatedly read into a temporary and push_back after every read) – Borgleader Mar 03 '17 at 14:46
  • @1201ProgramAlarm Notice that OP has read that link (as specified *in the actual question*) – Borgleader Mar 03 '17 at 14:48
  • Not sure if we should close it or not. They really should explain how that question did not work for them. – NathanOliver Mar 03 '17 at 14:51
  • @NathanOliver Seeing as read/write have nearly the same signature (const diff on char*), and OP seems to have it right on the `write` call, I'd almost close as typo? (even though its not technically a typo) I just find it silly to close as duplicate when OP has read said duplicate. – Borgleader Mar 03 '17 at 14:57
  • You can't read directly into a vector because you don't know where the `vector` class stores its data. Ask the `vector` for the address of its data, then read into that address. – Thomas Matthews Mar 03 '17 at 15:12
  • 1
    [Your struct is not a POD type](http://ideone.com/f8tYnK), thus you cannot just read a blob of bytes and expect it to magically be a vector of `CameraParams`. If you read the link you posted, the answer states that what to do if the type is not a POD type. – PaulMcKenzie Mar 03 '17 at 15:14
  • @Borgleader Not sure. The OP is also not writing out the size of the vector to the file and the is needed for the read to be successful as they need to read that in and allocate that amount of space. – NathanOliver Mar 03 '17 at 15:17
  • I will try reading the above answer and try to resolve it again but due to my very limited knowledge of C++ it is proving to be difficult. – raj Mar 03 '17 at 15:19
  • 1
    @NathanOliver Unless as I suggested you read one at a time (until eof) and push_back each successful read, the bigger problem as PaulMcKenzie pointed out, is that struct is apparently not a POD. – Borgleader Mar 03 '17 at 15:21
  • I tried scripts from link (check EDIT) but still I am missing something basic. Help! – raj Mar 03 '17 at 16:31
  • *Does not work* is helpless! Please give exact error message or describe problem... – Serge Ballesta Mar 03 '17 at 16:32
  • I have described the exact problem in the edit. Please check – raj Mar 04 '17 at 15:28
  • You really need to try and explain why you didn't understand the answers given in the duplicate question. It really *is* the same question! – Martin Bonner supports Monica Mar 04 '17 at 16:04
  • as I have mentioned in EDIT 1 and 2 that the script I am using for read and write function is from duplicate question. The way function will be defined are different because of structure and vector definition as mentioned by me earlier – raj Mar 04 '17 at 19:09

0 Answers0