0

I tried this method:

void read(vector<int> &vec, string filename)
{
    std::ifstream file(filename, std::ios::binary | std::fstream::binary);
    if (!file.is_open()) throw runtime_error("could not open file");
    //std::istreambuf_iterator<char> start(file), end;
    std::istream_iterator<int> start(file), end;
    //std::istreambuf_iterator<char> start(file), end;
    vec = vector<int>(start, end);
    file.close();
}
void write(vector<int> &vec, string filename) {
    std::ofstream file(filename, std::ios::binary | std::fstream::binary);
    if (!file.is_open()) throw runtime_error("could not open file");
    copy(
        vec.begin(),
        vec.end(),
        //std::ostreambuf_iterator<char>(file)
        std::ostream_iterator<int>(file)
    );
    file.close();
}
int main(){
    vector<int> vec =
    {124,5,543,6,756,6,3,234,5,5,765}, vec2;
    write(vec, "thing.bin");
    read(vec2, "thing.bin");
    for (auto&a : vec2) cout << a << endl;
    system("PAUSE");
}

The commented code are alternative iterators I tried.

The problem is that std::ostreambuf_iterator<int>(file) is an invalid iterator to a file. Do I need to do a reinterpret_cast? Am I obligated to use fstream .write() and read() ?

jokoon
  • 6,207
  • 11
  • 48
  • 85
  • 1
    I don't see anything particularly wrong with this code, except maybe that the `vec` argument to `write` could be const (which wouldn't affect the correct operation here). What is happening, and what do you expect to happen instead? – cdhowie Dec 07 '17 at 06:27
  • What is wrong with `file.write(vec.begin(), size);` ? ` – Sniper Dec 07 '17 at 06:36
  • 1
    @Sniper You mean aside from the fact that that won't compile, because `std::vector::iterator` isn't implicitly convertible to `char *`, and that wouldn't do what you wanted anyway? – cdhowie Dec 07 '17 at 06:45
  • 2
    It seems that you are not considering the endiannes of the binary representation of `int`, which could be fine if you are not planning to use the file in a different platform. Also: https://stackoverflow.com/q/1107705/4944425 – Bob__ Dec 07 '17 at 09:30

1 Answers1

0
void read(vector<int> &vec, string filename)
{
    std::ifstream file(filename, std::ios::binary | std::fstream::binary);
    if (!file.is_open()) throw runtime_error("could not open file");

    file.seekg(0, ios_base::end);
    int size = file.tellg() / sizeof(int);
    vec = vector<int>(size);
    file.seekg(0, file.beg);
    file.read(reinterpret_cast<char*>(vec.data()), size*sizeof(int));
    file.close();
}
void write(vector<int> &vec, string filename) {
    std::ofstream file(filename, std::ios::binary | std::fstream::binary);
    if (!file.is_open()) throw runtime_error("could not open file");
    file.write(reinterpret_cast<const char*>(vec.data()), vec.size() * sizeof(int));
    file.close();
}
int main(){
    vector<int> vec =
    {0,1,2,3,-1,127,128,1023,1024}, vec2;
    write(vec, "thing.bin");
    read(vec2, "thing.bin");
    for (auto&a : vec2) cout << a << endl;
    return 0;
}
jokoon
  • 6,207
  • 11
  • 48
  • 85