简体   繁体   English

C++ 传递一个 .txt 文件,它有一个函数名和参数

[英]C++ pass a .txt file which has a function name and the parameter

I am working on a simple homework that requires me to pass a .txt file which contains 2 lines, the first is the function name, the second is a number, 3, or 9 for example.我正在做一个简单的作业,需要我传递一个包含 2 行的 .txt 文件,第一行是函数名称,第二行是数字,例如 3 或 9。

I am asking how to pass argv[0] as the function name and argv[ 1] as the parameter.我在问如何将 argv[0] 作为函数名和 argv[1] 作为参数传递。

I'm not sure how to implement this so I get essentially what is in the first screen (I know that it is wrong).我不确定如何实现这一点,所以我基本上得到了第一个屏幕中的内容(我知道这是错误的)。

First screen shot, bad syntax but this is what I'm trying to do第一个屏幕截图,语法错误,但这就是我想要做的

I have started with what is in the second screen,我从第二个屏幕中的内容开始,

second screen shot, this seems like it should work, but I can't seem to test it第二个屏幕截图,这看起来应该可行,但我似乎无法对其进行测试

but I also can't figure out how to pass the .txt in the developer command window to test it.但我也无法弄清楚如何在开发人员命令窗口中传递 .txt 来测试它。 Using Windows Developer Command Prompt.使用 Windows 开发人员命令提示符。 (The program was created in Visual Studio 2015) I navigate to the debug folder, ...debug>Program "textFile" (该程序是在 Visual Studio 2015 中创建的)我导航到调试文件夹,...debug>Program "textFile"

This runs the program but does not seem to be taking the file这运行了程序,但似乎没有获取文件

I have done a TON of searching to try to find some answers, but so far have not found anything that answers this, found a lot of how to pass files for arguments and such, but nothing to address this specifically.我已经进行了大量搜索以试图找到一些答案,但到目前为止还没有找到任何答案,找到了很多如何传递参数等的文件,但没有专门解决这个问题。

You have two alternatives: 1) Pass the filename and number;您有两种选择:1)传递文件名和编号; or 2) Pass the file stream and number.或 2) 传递文件流和编号。

For example:例如:

void Read_File(const std::string filename,
               unsigned int number)
{
  std::ifstream input(filename.c_str());
  //...
}

void Read_From_Stream(std::istream& input,
                      unsigned int number)
{
  // ...
}

int main(void)
{
  Read_File("my_file.txt", 24);
  std::ifstream another_file("your_text_file.txt);
  Read_From_Stream(another_file, 42);
  // ...
  return EXIT_SUCCESS;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM