【问题标题】:Opening a text file from a command line argument从命令行参数打开文本文件
【发布时间】:2014-10-26 08:30:30
【问题描述】:

我正在尝试从命令行参数打开一个文件。

我已经调试了我的程序:

当我打印文件的值时,它给出的值是。

当我打印 argv 的值时,它给出的值 (char **) 0x7ffffffffe4e0。

当我打印 argv[1] 的值时,它给出的值是 0x0。

该函数不会打开我的文件。不知道为什么?

我试过了:

if (file.is_open()) 也是同样的问题。

在我的主要功能中,我通过了:

buildBST(&argv[1]);

BSTTreeData buildBST (char *argv[]){
  vector <string> allwords;

  BSTTreeData Data;
  BinarySearchTree<string> Tree;

  ifstream file (argv[1]);

  char token;

  string listofchars = "";
  string input;

  int distinct = 0;
  int line = 1;

  if (file){
      //if the file opens
      while (getline(file, input)) //gets every line in the file
      {
          for (int i = 0; i < input.size(); ++i) //gets all the contents in the line
          {
              token = input[i]; //each character become a 'token'
              if (isalpha(token))
              {
                  //if the character is an alphabetical character
                  listofchars += token; //append character to a string
                  if (Contains(allwords, listofchars) == false)
                  {
                  //if the current word has not already been added to vector of words
                      //increment the distinct word count
                      distinct += 1;
                      Tree.insert(listofchars); //creates the BST
                      allwords.push_back(listofchars); 
                      //add current word to vector of all the words
                  }
                  else
                  line++; //increments the line number
              }
              else
                  line++; //increments the line number
          }
          listofchars = ""; //creates empty character string
      }
    }
    file.close(); //closes file

    Data.BST = Tree;
    Data.linenumber = line;
    Data.distinctwords = distinct;
    Data.words = allwords;
    return Data;
}

【问题讨论】:

  • 如果argv[1]NULL,那么问题在于您没有将文件名作为第一个参数传递。你是如何运行程序的?
  • 我使用 'g++ -g -std=c++11 myfile.cpp' 编译并执行 './a.out test.txt'
  • 等等,你将&amp;argv[1] 传递给buildBST,这意味着buildBST 中的argv 参数现在指向第一个参数。这意味着argv[1]buildBST 指的是第二个参数,而不是第一个! (请记住,C 中的索引是从零开始的。)
  • 好吧,这是有道理的。那么你的意思是我应该将 argv[0] 传递给 buildBST?并更改'ifstream file (argv[0]);'?
  • arg[0] 通常包含应用程序的路径,而不是传递给它的第一个参数(即/bin/appname)。

标签: c++ text-files command-line-arguments ifstream


【解决方案1】:

回想一下,在大多数操作系统中,main 函数中的argv 是以下形式的数组:

argv[0]        = /* path to the program ("zeroth argument") */;
argv[1]        = /* first argument */;
argv[2]        = /* second argument */;
...
argv[argc - 1] = /* last argument */;
argv[argc]     = NULL;

这里的问题是您正在查看 second 参数,而不是第一个参数。当您在 main 函数中调用 buildBST(&amp;argv[1]) 时,会将指针移动一个元素,以便 within buildBST argv 现在指向第一个参数而不是第零个参数,因此buildBST 中的表达式argv[1] 产生第二个参数而不是第一个参数。

解决办法是:

  1. &amp;argv[0](或等效地,只是argv)传递给buildBST,然后使用argv[1]获取参数,或者

  2. &amp;argv[1](或等效的argv + 1)传递给buildBST,然后使用argv[0]获取参数。

第一种方法可能更具可读性,因为您没有改变 argv 的含义。

【讨论】:

  • 我认为第二种方法是最好的,因为您可以简单地通过将 argv[2] 传递给函数来重用代码。当然,我也不会传递数组,只传递指向字符串的指针。
  • 您也可以这样做,但正如您所说,在这种情况下,您不应该传入 char ** 开头,当然也不应该标记变量 argv
  • 执行此操作后,我收到“控件可能到达非无效函数的结尾”错误
  • 你忘了把return XXX;放在什么地方吗?
猜你喜欢
  • 2012-03-15
  • 2013-05-18
  • 2018-11-08
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2022-12-17
  • 2012-02-21
  • 2020-03-22
相关资源
最近更新 更多