在启动时为应用程序提供一些数据

Giving the application some data on launch

本文关键字:数据 应用程序 启动      更新时间:2023-10-16

我正在尝试创建一个应用程序,可以从另一个程序启动时接收一些数据。例如:

Start_App.exe calls Main_App.exe and gives it the current date, all at the same time
(while launching it)
Main_App.exe outputs the date on its console

如果没有Start_App传递的数据,其他程序就不能正常工作或将做其他事情。我一直在找,但似乎我错过了技术名称…

您可能需要使用命令行参数。
它们通过直接在程序名之后用空格分隔的方式写出来传递。

像这样:

#include <iostream>
int main(int argc, char *argv[])
{
    using namespace std;
    cout << "There are " << argc << " arguments:" << endl;
    // Loop through each argument and print its number and value
    for (int nArg=0; nArg < argc; nArg++)
        cout << nArg << " " << argv[nArg] << endl;
    return 0;
}

argc是程序接收到的参数个数。
*argv[]是字符串数组,每个参数对应一个字符串。

如果你像这样调用程序:

Program.exe arg1 arg2 arg3

它给你:

There are 3 arguments:
0 arg1
1 arg2
2 arg3