Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Бинарный файл


Автор: X 1.11.2005, 03:46
Имеется некая структура, под которую динамически выделяется память. Ввод данных происходит с клавиатуры, данные нужно записать в бинарный файл, как это сделать? Причем если scaner[i].price >= 200, тогда записывать в файл, если меньше, то не записывать.

Код

struct scan_info
         {
       char mode[25];
       int price;
       double x_size;
       double y_size;
       int optr;
       int grey;
       };

//----------------------------------------------------
void input_information();
    {
   int count = 0;
   int count_price = 0;
   cout << "Input number of records: ";
   cin  >> count;
   scan_info* scaner = new scan_info[count];
   for (int i=0; i < count; i++)
      {
    cout << "\nInput information about scaner\n";
      cout << "Input model ";  cin >> scaner[i].mode;   cout "\n";
      cout << "Input price ";  cin >> scaner[i].price;  cout "\n";
      cout << "Input x_size "; cin >> scaner[i].x_size; cout "\n";
      cout << "Input y_size "; cin >> scaner[i].y_size; cout "\n";
      cout << "Input optr ";   cin >> scaner[i].optr;   cout "\n";
      cout << "Input grey ";   cin >> scaner[i].grey;   cout "\n";
      if (scaner[i].price >= 200) count_price++;
   }

Автор: Void 1.11.2005, 08:50
Код
std::ofstream out("filename.dat", std::ios_base::binary);
// ...
out.write(reinterpret_cast<char*>(&scaner[i]), sizeof(scan_info));

Автор: Helicopterr 4.11.2005, 22:48
std::ios_base::binary
ощутимых различий с обычным выводом не даёт

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)