Хорошо ли мое решение для работы с файлами?
RAII для управления ресурсами - это хороший и рекомендуемый способ.
Однако с RAII область действия (и, следовательно, время жизни) переменных должна рассматриваться еще более тщательно.
В открытом коде OP:
int main()
{
oFile file("test.ndf");
double b = 12.;
file.write_value(b);
std::vector<double> v{1., 2., 3., 4., 6., 10., 15.};
file.write_vector(v);
iFile ifile("test.ndf");
double ib;
ifile.read_value(ib);
std::cout << ib << std::endl;
std::vector<double> iv;
ifile.read_vector(iv);
std::cout << "vector size: " << iv.size() << std::endl;
for(auto val : iv){
std::cout << val << " ";
}
std::cout << "last val: " << iv[iv.size() - 1] << std::endl;
} // <-- all local variables incl. file will be destroyed here
экземпляр ofile file
живет до конца main
.
wrote a value: 12 current offset: 8
wrote a value: 7 current offset: 16
wrote a value: 1 current offset: 24
wrote a value: 2 current offset: 32
wrote a value: 3 current offset: 40
wrote a value: 4 current offset: 48
wrote a value: 6 current offset: 56
wrote a value: 10 current offset: 64
wrote a value: 15 current offset: 72
value read: 12 current offset: 8
12
value read: 7 current offset: 16
size of vector: 7
value read: 1 current offset: 24
value read: 2 current offset: 32
value read: 3 current offset: 40
value read: 4 current offset: 48
value read: 6 current offset: 56
value read: 10 current offset: 64
value read: 10 current offset: 72
vector size: 7
1 2 3 4 6 10 10 last val: 10
Live Demo на coliru (где я воспроизвел проблему OP).
как внутренний поток закрыто в деструкторе ofile
, до этой точки может быть внутренне буферизованное (т.е. не очищенное) содержимое, хотя поток сообщит о них как записанный.
Решение простое: Область действия ofile file
должно быть ограничено.
Фиксированный код:
int main()
{
std::vector<double> v{1., 2., 3., 4., 6., 10., 15.};
{ // start new scope
oFile file("test.ndf");
double b = 12.;
file.write_value(b);
file.write_vector(v);
} // close scope -> destroy file (and b)
iFile ifile("test.ndf");
double ib;
ifile.read_value(ib);
std::cout << ib << std::endl;
std::vector<double> iv;
ifile.read_vector(iv);
std::cout << "vector size: " << iv.size() << std::endl;
for(auto val : iv){
std::cout << val << " ";
}
std::cout << "last val: " << iv[iv.size() - 1] << std::endl;
}
Выход:
wrote a value: 12 current offset: 8
wrote a value: 7 current offset: 16
wrote a value: 1 current offset: 24
wrote a value: 2 current offset: 32
wrote a value: 3 current offset: 40
wrote a value: 4 current offset: 48
wrote a value: 6 current offset: 56
wrote a value: 10 current offset: 64
wrote a value: 15 current offset: 72
value read: 12 current offset: 8
12
value read: 7 current offset: 16
size of vector: 7
value read: 1 current offset: 24
value read: 2 current offset: 32
value read: 3 current offset: 40
value read: 4 current offset: 48
value read: 6 current offset: 56
value read: 10 current offset: 64
value read: 15 current offset: 72
vector size: 7
1 2 3 4 6 10 15 last val: 15
Live Demo on coliru
Я не понимаю, почему он записывает или читает последнее значение 2 раза?
Это не было ясно Мне тоже, пока я не понял, что слишком сосредоточился на , прочитал последнее значение 2 раза .
На самом деле это не так - он просто не может прочитать последнее значение и снова ошибочно сообщает предыдущее значение .
Чтобы проверить это, я добавил минимальную «обработку ошибок» в iFile::read_value
:
template <typename T>
void read_value(T& v){
file.seekg(offset);
file.read(reinterpret_cast<char*> (&v), sizeof(T));
if (!file) std::cerr << "AARG! Input failed. :-(\n";
offset += sizeof(T);
std::cout << "value read: " << v << " current offset: " << offset << std::endl;
}
Вывод:
wrote a value: 12 current offset: 8
wrote a value: 7 current offset: 16
wrote a value: 1 current offset: 24
wrote a value: 2 current offset: 32
wrote a value: 3 current offset: 40
wrote a value: 4 current offset: 48
wrote a value: 6 current offset: 56
wrote a value: 10 current offset: 64
wrote a value: 15 current offset: 72
value read: 12 current offset: 8
12
value read: 7 current offset: 16
size of vector: 7
value read: 1 current offset: 24
value read: 2 current offset: 32
value read: 3 current offset: 40
value read: 4 current offset: 48
value read: 6 current offset: 56
value read: 10 current offset: 64
AARG! Input failed. :-(
value read: 10 current offset: 72
vector size: 7
1 2 3 4 6 10 10 last val: 10
Демонстрация в реальном времени на coliru
Конечно, файловый ввод-вывод может быть неудачным по разным причинам. Итак, успешность файловых операций должна проверяться всегда.