1

I'm working on a project that involves binary files. So I started researching about binary files but I'm still confused about how to write and fill a vector from that binary file that I wrote before

Here's code: for writing.

void binario(){
ofstream fout("./Binario/Data.AFe", ios::out | ios::binary);
vector<int> enteros;
enteros.push_back(1);
enteros.push_back(2);
enteros.push_back(3);
enteros.push_back(4);
enteros.push_back(5);
//fout.open()
//if (fout.is_open()) {
    std::cout << "Entre al if" << '\n';
    //while (!fout.eof()) {
        std::cout << "Entre al while" << '\n';
        std::cout << "Enteros size: "<< enteros.size() << '\n';
        int size1 = enteros.size();
        for (int i = 0; i < enteros.size(); i++) {
            std::cout << "for " << i << '\n';
            fout.write((char*)&size1, 4);
            fout.write((char*)&enteros[i], size1 * sizeof(enteros));
            //cout<< fout.get(entero[i])<<endl;
        }
        //fout.close();
    //}
    fout.close();
    cout<<"copiado con exito"<<endl;
//} 
}

Here's code for reading:

oid leerBinario(){
vector<int> list2;

ifstream is("./Binario/Data.AFe", ios::binary);
    int size2;
    is.read((char*)&size2, 4);
    list2.resize(size2);


    is.read((char*)&list2[0], size2 * sizeof(list2));

    std::cout << "Size del vector: " << list2.size() <<endl;
    for (int i = 0; i < list2.size(); i++) {
        std::cout << i << ". " << list2[i] << '\n';
    }
    std::cout << "Antes de cerrar" << '\n';
    is.close();
}

I don't know if I'm writing correctly to the file, this is just a test so I don't mess up my main file, instead of writing numbers I need to save Objects that are stored in a vector and load them everytime the user runs the program.

2
  • Reading back the file that you wrote and getting the same values that you wrote would be a good test of whether you are writing to the file correctly. Commented Mar 9, 2017 at 4:06
  • 2
    I don't understand why you write enteros.size() for every single vector element instead of just writing it once. And when you write enteros[i] you definitely don't want enteros.size()*sizeof(enteros) because that is 5 times the bytes in the entire vector - you probably want sizeof(enteros[i]) Commented Mar 9, 2017 at 4:10

1 Answer 1

1

Nope, you're a bit confused. You're writing the size in every iteration, and then you're doing something completely undefined when you try to write the value. You can actually do this without the loop, when you are using a vector.

fout.write(&size1, sizeof(size1));
fout.write(enteros.data(), size1 * sizeof(int));

And reading in:

is.read(&list2[0], size2 * sizeof(int));

To be more portable you might want to use data types that won't change (for example when you switch from 32-bit compilation to 64-bit). In that case, use stuff from <cctype> -- e.g. int32_t for both the size and value data.

Sign up to request clarification or add additional context in comments.

1 Comment

This really helped me, thanks. I have another question I needed to cast to (char*) why do I need to do this, if i take it out it doesn't compile. Also, for writing and reading objects it would be the same thing, just replacing fout.write((char*)&size1, sizeof(size1)); fout.write((char*)OBJVECTOR.data(), size1 * sizeof(OBJECTNAME)); Thanks for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.