⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datascan.cpp

📁 Think in C++ 第二版源码
💻 CPP
字号:
//: C18:Datascan.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

//{L} Datalog

// Verify and view logged data

#include "DataLogger.h"

#include "../require.h"

#include <iostream>

#include <fstream>

#include <strstream>

#include <iomanip>

using namespace std;



int main() {

  ifstream bindata("data.bin", ios::binary);

  assure(bindata, "data.bin");

  // Create comparison file to verify data.txt:

  ofstream verify("data2.txt");

  assure(verify, "data2.txt");

  DataPoint d;

  while(bindata.read(

    (unsigned char*)&d, sizeof d))

    d.print(verify);

  bindata.clear(); // Reset state to "good"

  // Display user-selected records:

  int recnum = 0;

  // Left-align everything:

  cout.setf(ios::left, ios::adjustfield);

  // Fixed precision of 4 decimal places:

  cout.setf(ios::fixed, ios::floatfield);

  cout.precision(4);

  for(;;) {

    bindata.seekg(recnum* sizeof d, ios::beg);

    cout << "record " << recnum << endl;

    if(bindata.read(

      (unsigned char*)&d, sizeof d)) {

      cout << asctime(&(d.getTime()));

      cout << setw(11) << "Latitude"

           << setw(11) << "Longitude"

           << setw(10) << "Depth"

           << setw(12) << "Temperature"

           << endl;

      // Put a line after the description:

      cout << setfill('-') << setw(43) << '-'

           << setfill(' ') << endl;

      cout << setw(11) << d.getLatitude()

           << setw(11) << d.getLongitude()

           << setw(10) << d.getDepth()

           << setw(12) << d.getTemperature()

           << endl;

    } else {

      cout << "invalid record number" << endl;

      bindata.clear(); // Reset state to "good"

    }

    cout << endl

      << "enter record number, x to quit:";

    char buf[10];

    cin.getline(buf, 10);

    if(buf[0] == 'x') break;

    istrstream input(buf, 10);

    input >> recnum;

  }

} ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -