简体   繁体   中英

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.

I am asking how to pass argv[0] as the function name and argv[ 1] as the parameter.

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. Using Windows Developer Command Prompt. (The program was created in Visual Studio 2015) I navigate to the debug folder, ...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; or 2) Pass the file stream and number.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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