如何在 Protocol Buffer C++ 教程中编写消息?

How do I write a message in the Protocol Buffer C++ tutorial?

我是 Protocol Buffers 的新手,对 C++ 没有经验,我想在 https://developers.google.com/protocol-buffers/docs/cpptutorial

完成教程

我已经创建了教程中提到的原型文件,并从该原型中获得了 addressbook.pb.haddressbook.pb.cc。我正在尝试关注 "Writing A Message" 段,因此我从教程中复制并粘贴了以下代码。我立即 运行 进入主要功能中的问题,我将在下面解释:

#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;

// This function fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
  cout << "Enter person ID number: ";
  int id;
  cin >> id;
  person->set_id(id);
  cin.ignore(256, '\n');

  cout << "Enter name: ";
  getline(cin, *person->mutable_name());

  cout << "Enter email address (blank for none): ";
  string email;
  getline(cin, email);
  if (!email.empty()) {
    person->set_email(email);
  }

  while (true) {
    cout << "Enter a phone number (or leave blank to finish): ";
    string number;
    getline(cin, number);
    if (number.empty()) {
      break;
    }

    tutorial::Person::PhoneNumber* phone_number = person->add_phone();
    phone_number->set_number(number);

    cout << "Is this a mobile, home, or work phone? ";
    string type;
    getline(cin, type);
    if (type == "mobile") {
      phone_number->set_type(tutorial::Person::MOBILE);
    } else if (type == "home") {
      phone_number->set_type(tutorial::Person::HOME);
    } else if (type == "work") {
      phone_number->set_type(tutorial::Person::WORK);
    } else {
      cout << "Unknown phone type.  Using default." << endl;
    }
  }
}

// Main function:  Reads the entire address book from a file,
//   adds one person based on user input, then writes it back out to the same
//   file.
int main(int argc, char* argv[]) {
  // Verify that the version of the library that we linked against is
  // compatible with the version of the headers we compiled against.
  GOOGLE_PROTOBUF_VERIFY_VERSION;

  if (argc != 2) {
    cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
    return -1;
  }

  tutorial::AddressBook address_book;

  {
    // Read the existing address book.
    fstream input(argv[1], ios::in | ios::binary);
    if (!input) {
      cout << argv[1] << ": File not found.  Creating a new file." << endl;
    } else if (!address_book.ParseFromIstream(&input)) {
      cerr << "Failed to parse address book." << endl;
      return -1;
    }
  }

  // Add an address.
  PromptForAddress(address_book.add_person());

  {
    // Write the new address book back to disk.
    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    if (!address_book.SerializeToOstream(&output)) {
      cerr << "Failed to write address book." << endl;
      return -1;
    }
  }

  // Optional:  Delete all global objects allocated by libprotobuf.
  google::protobuf::ShutdownProtobufLibrary();

  return 0;
}

在 main 函数中,由于这部分代码退出而不提示任何输入:

  if (argc != 2) {
    cerr << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
    return -1;
  }

在请求任何输入之前显示为函数中的前几行之一。我读到 argc 将是输入的数量。我很困惑,因为我完全复制并粘贴了教程中写的内容,但它似乎 运行ning 不正确。

该代码需要在命令行上传递文件名,而不是从标准输入中读取。您没有指定您使用的平台,但您可以在 Windows 上执行 my_program.exe C:\some\file\somewhere 或在 Linux/Mac/Other Unix 上执行 ./my_program /some/file/somewhere,例如 OS。如果您 运行 来自 IDE 的 run/debug 函数的程序,那么您需要将其配置为将文件名作为命令行参数传递。如何做到这一点将取决于您使用的 IDE。