Jump to content

C++ - Command line input parameter * (asterisk)

Guest Machines
Go to solution Solved by rashdanml,

You'd need to find a way of escaping special characters using \. I'm not sure if C++ has a built in function to do that, so a way around it would be to loop through the provided input and replace known special characters with \[character].

Code:

#include <iostream>#include <cstdlib>using namespace std;int main(int argc, char** argv){cout << endl;for (int i=0;i<argc;i++){cout << "i" << ". "<< argv[i] << " " << endl; }} 

Input: 1 * 2

Output:
0. program.exe
1. 1
2. program.cpp
3. program.exe
4. 2

What do I have to change to make output look like this:
0. program.exe
1. 1
2. *
3. 2

 

Link to comment
Share on other sites

Link to post
Share on other sites

Input can't be changed. I'm looking for a way to change the code.

Link to comment
Share on other sites

Link to post
Share on other sites

Input can't be changed. I'm looking for a way to change the code.

impossible

* is a jolly character for the OS, it will take it and substitute it with a list of all the files in the program's folder

when the data arrives to your program, everything is already wrong

Link to comment
Share on other sites

Link to post
Share on other sites

impossible

* is a jolly character for the OS, it will take it and substitute it with a list of all the files in the program's folder

when the data arrives to your program, everything is already wrong

Yes, I know that * character puts names of the files into array.

Impossible? Really? No one came up with solution better than this - int argc, char** argv?

Link to comment
Share on other sites

Link to post
Share on other sites

You'd need to find a way of escaping special characters using \. I'm not sure if C++ has a built in function to do that, so a way around it would be to loop through the provided input and replace known special characters with \[character].

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Impossible? Really? No one came up with solution better than this - int argc, char** argv?

it's not a solution, it's just how the OS passes arguments and how every program takes it, by the time you acces that array, the data is lost

at least under unix

anyway, it looks like windows does things differently and you can somehow retrieve the original command line

 

http://msdn.microsoft.com/en-us/library/windows/desktop/ms683156%28v=vs.85%29.aspx

 

edit:

i'm not sure that GetCommandLine() will give you the original arguments, i just read that windows lets you do it somehow, but i don't know why

where does the data come from?

Link to comment
Share on other sites

Link to post
Share on other sites

where does the data come from?

Guessing you mean where the input comes from.

 

The program is called from the commandline:

test.exe [inputs]

 

where [inputs] = 1 * 2

 

Program parses the inputs and gives the output. 

 

In Linux: (I backslashed in manually to get it to work)

[rashdan@rashdanml ~]$ ./test 1 \* 2 0. ./test 1. 1 2. * 3. 2

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Guessing you mean where the input comes from.

The program is called from the commandline:

test.exe [inputs]

where [inputs] = 1 * 2

Program parses the inputs and gives the output.

In Linux:

[rashdan@[member='rashdanml'] ~]$ ./test 1 \* 2 0. ./test 1. 1 2. * 3. 2
the problem is that, as the OP said, the input cannot be changed, so i want to know why, it could be useful to know what program is calling that command line (so that we can maybe find a workaround to change the arguments format)
Link to comment
Share on other sites

Link to post
Share on other sites

the problem is that, as the OP said, the input cannot be changed, so i want to know why, it could be useful to know what program is calling that command line (so that we can maybe find a workaround to change the arguments format)

Well, true, the input cannot be changed manually, but it can still be modified by escaping special characters. That can either be done before the program is called (passing in the input with \ added in), or after (modifying the input with \). Either way will fix the problem. 

 

In the above code, it's shouldn't be difficult to loop through argv and adding \ before special characters. 

Interested in Linux, SteamOS and Open-source applications? Go here

Gaming Rig - CPU: i5 3570k @ Stock | GPU: EVGA Geforce 560Ti 448 Core Classified Ultra | RAM: Mushkin Enhanced Blackline 8GB DDR3 1600 | SSD: Crucial M4 128GB | HDD: 3TB Seagate Barracuda, 1TB WD Caviar Black, 1TB Seagate Barracuda | Case: Antec Lanboy Air | KB: Corsair Vengeance K70 Cherry MX Blue | Mouse: Corsair Vengeance M95 | Headset: Steelseries Siberia V2

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Well, true, the input cannot be changed manually, but it can still be modified by escaping special characters. That can either be done before the program is called (passing in the input with \ added in), or after (modifying the input with \). Either way will fix the problem.

In the above code, it's shouldn't be difficult to loop through argv and adding \ before special characters.

argv doesn't contain any asterisks

it contains the list of files in the dir, try running it, there is nothing left to escape

Link to comment
Share on other sites

Link to post
Share on other sites

I've tried this http://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx

and it works.

#include <windows.h>#include <stdio.h>#include <shellapi.h>int __cdecl main(){   LPWSTR *szArglist;   int nArgs;   int i;   szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);   if( NULL == szArglist )   {      wprintf(L"CommandLineToArgvW failed\n");      return 0;   }   else for( i=0; i<nArgs; i++) printf("%d: %ws\n", i, szArglist[i]);// Free memory allocated for CommandLineToArgvW arguments.   LocalFree(szArglist);   return(1);}

The program shows correctly output (under Windows) without adding any characters to the input 1 * 2:

 

0. program.exe
1. 1
2. *
3. 2

Link to comment
Share on other sites

Link to post
Share on other sites

You could also try using a shell that doesn't expand to a list of files. I can't think of any off hand but that is certainly one possibility as this is something the shell and the OS are doing and not something your program is doing.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×