Click here to Skip to main content
15,037,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Qt application for which i have to pass some command line arguements to make it execute. In the start user need to Type the FileName. Than i need to look for this file in file directory.
This is my incomplete code.
C#
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    QTextStream stream(stdin); //enter the file name
    QString UserFileName = stream.readLine();
// is it the correct method to get the file name from user in command line??
// if yes than how to add this value to argv[]??
    QStringList args = app.arguments();
    if (args.count() != 2)
    {
        std::cerr << "argument required" << endl;
        //return 1;
    }
    
    WorkerThread WThreadObj;
    WThreadObj.StartFileAnalysis(args[1]);   //pass cmd 2nd argument
    return app.exec();
}



Thanks for reading the question
Posted

1 solution

The arguments are passed to the main function where argc specifies the number of arguments and argv is the list of arguments. The first argument argv[0] is the name of the command itself so that argc will be at least 1.

These parameters are passed to the QCoreApplication constructor that initialises an internal QStringList with the arguments.

You can access the arguments by using argv (char * with platform specific encoding) or QApplication::arguments() (QStringList Unicode).

So there is no need to ask for the file name as in your code. However, you might do so if a required argument was not present.
   
Comments
XamBEE 19-Jan-16 10:13am
   
How i can pass the file name as argument to main function?
Jochen Arndt 19-Jan-16 10:18am
   
It is passed by the system when executing the program.

At the Windows command prompt / in an Unix shell type:
'program_name argument1 argument2'

Similar applies to Windows batch and command files, and Unix shell scripts.

With a Windows link (e.g. on the desktop), append the arguments separated by spaces to the executable name.
XamBEE 19-Jan-16 10:31am
   
Sorry I know my question is very stupid but i dont know so i am asking..
I am Using QT Creator. When i run program I get only the default argument argv[0] because I am not running the program from command line
Jochen Arndt 19-Jan-16 10:38am
   
You have two options:
- Start the program from a command prompt / shell,
- Edit your project settings to specify additional arguments

I have just shut down my VM with QtCreator so I must guess a little bit:
- In the Build&Run tab select Run
- You should have a window 'Run Settings'
- There should be an input field for 'Arguments'

The second method is only useful when the arguments will not change. Otherwise you have to edit the settings each time before executing.
CPallini 19-Jan-16 16:24pm
   
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900