我想采用运行时输入而不是命令行参数,怎么做?

时间:2016-11-26 16:21:20

标签: c++

我有这段代码,

int main(int argc, char *argv[])
{

  //################################
  // Variables
  //################################

  // common
  int num_of_params; // number of command-line parameters

  // related to files
  //string data_filename;
  char data_filename[CHUNK_BUF_SIZE];              // input file path

  int data_filesize = 0;        // data file size

  // related to chunks
  int fd;                       // file descriptor to check boundary
  int size;                     // size of input data
  unsigned char *buf;           // input data

  int offset      = 0;        // file offset
  int chunk_b_pos = 0 ;       // beginning position of a chunk in a file
  int chunk_e_pos = -1;       // ending position of a chunk in a file
                                // *** NOT 0 ** due to "set_breakpoint() in chunk_sub.c"
                                //
  int cur_chunk_size = 0;       // !!!! Accumulated chunk size !!!!
                                // This value is compared to minimum chunk size and maximum chunk size

  int chunk_index_fd;           // file which contains chunk indexes for a document
  char chunk_index_filepath[CHUNK_BUF_SIZE];     // chunk index file path


  // parameters
  int avg_chunk_size;           // expected averge chunk size
  int min_chunk_size;           // minimum chunk size
  int max_chunk_size;           // maximum chunk size

  // temporary command
  char cmd[CHUNK_BUF_SIZE];

  int num_of_breakpoints = 0;


  //################################
  // Check parameters
  //################################
  // get number of command-line parameters
  num_of_params = argc - 1;

  if (num_of_params != 4)
  {
    printf("usage : %s <input file> <expected avg chunk size> <min chuk size> <max chunk size>\n",
            argv[0]);
    printf("e.g.    %s body 8192 2048 65535\n", argv[0]);
    printf("***     try to change 1024 to 2048, 4096, and 8192, and see results\n");
    exit(1);
  }


  strcpy(data_filename, argv[1]); // input data filename
  avg_chunk_size = atoi(argv[2]);
  min_chunk_size = atoi(argv[3]);
  max_chunk_size = atoi(argv[4]);

这个程序采用命令行参数,

例如。 &gt;块体8192 2048 65535

其中body是文件名,其他是块参数

我的问题是我不想要命令行参数,我想在运行程序时接受输入。怎么做。

1 个答案:

答案 0 :(得分:0)

以下是如何使用C ++进行运行时输入的示例:

#include <iostream>
#include <cstdlib>
int main(void)
{
  std::cout << "Enter a number: ";
  int number;
  std::cin >> number;
  std::cout << "\nYour number is: " << number << "\n";

  std::cout << "\n\nPaused.  Press Enter to continue.\n";
  std::cin.ignore(1000000, '\n'); // Wait for Enter to be pressed.
  return EXIT_SUCCESS;
}

&#34; std::cin >> number&#34;是如何使用C ++从标准输入输入。

要从文件中读取,请使用std::ifstream创建文件。有关更多示例,请在互联网上搜索&#34; c ++ ifstream打开文件&#34;。