Задать вопрос
@likilix
Лицемер

Почему не записываются данные в выходной поток итератором ostream...?

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <exception>

using namespace std;

    const std::string student_dbfile = "D:\\db.txt";
    const char *delim = "\n";

    class error_open_file
         : public exception
    {
     public:
        error_open_file(const std::string text)
           : exception(text.c_str())
        {

        }
    };


    struct Student
    {
     friend ostream& operator << (ostream& ost, const Student& student)
     {
      return (ost<<student.Name<<" "<<student.Surname<<" "<<student.Login);
     }

     friend istream& operator >> (istream& ist, Student& student)
     {
      return (ist>>student.Name>>student.Surname>>student.Login);
     }

     std::string Name;
     std::string Surname;
     std::string Login;

     Student() = default;
    };

int main()
{
  try
  {
   fstream io(student_dbfile, ios::in | ios::out); //| ios::ate

   if (io.is_open() == false)
      {
       throw error_open_file("File " + student_dbfile + " not found");
      }

   vector< Student > Students;

   copy(std::istream_iterator< Student >(io),
        std::istream_iterator< Student >(),
        std::inserter(Students, Students.begin()));
 
// Вот эти данные заменят старые и должны быть записаны в файл
   Students[0].Login = "123";
   Students[0].Name = "fdsfsdf";
   Students[0].Surname = "43243";

   copy(Students.begin(),
        Students.end(),
        std::ostream_iterator< Student >(cout, " "));

   io.seekg(0);

   copy(Students.begin(),
        Students.end(),
        std::ostream_iterator< Student >(io, " "));

   io.flush();
   io.close();
 }
 catch(error_open_file& eof)
 {
  cout<<eof.what();
 }

  return 0;
}
  • Вопрос задан
  • 37 просмотров
Подписаться 1 Простой Комментировать
Пригласить эксперта
Ответы на вопрос 1
@Furax
Элемент Students[0] существует ли после чтения из файла? Есть подозрение, что вектор Students пустой.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы