Monday, May 16, 2011

Conversion of QString/string to char */const char * and use it in system()

I always learn some small tricks which are simple but sometime time taking to crack it. So, I always decided to write a blog post on my understandings and tricks which will help people like me in future.

Problem Statement: Need to run an executable file/commands e.g "DIR" command (command line input: Any Folder Path ) from a parent cpp module "sysOut.cpp"using "system()" function. For example, I want to run the following command from my parent cpp module.
system("dir samples\task");
So, I want to take "folder path" as command line argument like,

> sysOut.exe samples/task (Using Backslash '/' in windows also, for details, please refer my previous POST which explained the reason of why using it)
We will also use 'QString' or 'string' for doing some string operations and using that into our program too.

SOLUTION:

/* Program: sysOut.cpp */
#include
#include
using namespace std;

int main(int argc, char **argv){

char* pathName = argv[1];
char exec[50];

cout << "Input Path Name: " << pathName << endl;

sprintf(exec, "dir %s", pathName);

cout << "********Exec Line: " << exec << endl << endl;

system(exec);

return 0;
}

You will notice one thing that I am using 'sprintf' for writing into formatted string, so that value of fileName is present in the 'exec' char string. If we do use 'sprintf' then system will consider fileName as the original Argument instead of the Path assigned to it.

Another Solution: using QString/string conversion to char */const char *

/* Program: sysOut.cpp */
#include
#include
using namespace std;

int main(){

string fileName;

fileName="blabla.txt";

const char * fpPtr = fileName.data();

char exec[50];

sprintf(exec, "parseOld.exe %s", fpPtr);

cout << "Exec String: " << exec << endl;

system(exec);

return 0;
}

You will notice in this module that there is 'blabla.txt' file is been saved into 'string'. You need to convert 'string' to 'char */const char *' to make is usable for 'sprintf'.

So, we save convert the string into 'const char *' using string::data function declared in string library. It returns 'const char *'. So, we will save the returned data to const char * fpPtr variable as shown in the program.

And when you compile the modules, it will give back the result as expected.

You may also refer a nice post on a Forum asked by some person:

C++, system(), and variables


Hope this may help you. Thank you.

Sunday, May 8, 2011

10 Reasons – Why Osama Bin Laden got killed?

10 Reasons – Why Osama Bin Laden got killed?

Because:

- Manmohan Singh is not President of US.

- Barack Obama doesn’t take permissions from Sonia Gandhi.

- He was not hiding in India. Our system is unable to find a missing Chief Minister in own country, can you expect them to find Osama.

- He didn’t surrender himself to Indian government. This government is not able to give death sentence to already arrested and convicted by the Supreme Court- Ajmal Kasab (eventually he would be by the SC) and Afzal Guru - after years. Had he surrendered to India, Osama could have spent a life here as our guest on our money.

- CBI was not incharge of investigation and operation. They are busy in saving Kalmadi, Kanimozhi and Raja.
- Amar Singh didn’t leak the tape of Obama’s phone though he could fake one.

- In Pakistan, he was not in minority community (like in India), so no human right activist and secular journalist came to save him.

- He didn’t meet Ekta Kapoor. She could have given him tip to be alive again.

- He didn’t request Rajnikant to save him.

- Last but not the least. He trusted Pakistan ISI and Government (Except common people (Aam Janta) from Pakistan).


Thursday, May 5, 2011

system() not recognizing path (with \\) using g++ complier for windows

I was using system() function
"int system ( const char * command ); "

for executing an executable external program from CPP program.
I want to give absolute and relative Path to the folder where executable was present.

I was using/calling the system function in the program name "testExt.cpp"

#include
int main(){


system("dir C:\\blabla");
system("C:\\blabla\\xyz.exe");
return 0;


}
Then compile using g++

g++ -g testExt.cpp -o teste

then ran the executable file "teste.exe" which we compiled above.

c:\> teste.exe
dir: C:blabla: No such file or directory
C:blablaxyz.exe: not found


Error. WHY?

You will notice one very surprising thing in that the backslash is not present in any of the command output. So, it will not follow the exact PATH you mentioned in the program. Even you try to write your path like this to fix the problem.

system("\"C:\\blabla\\xyz.exe\"");

It will still not help as it will still give different error message 'not found' instead of correct path and correct name of executable. Following the error message from the above another way of writing Path to the executable

C:\blabla\xyz.exe: not found

SOLUTION:

The main reason behind is that g++ compiler understand path in terms of UNIX/LINUX based system. So, lets re write "
testExt.cpp" Program.
#include
int main(){

system("dir /blabla");
system("/blabla/xyz.exe");

return 0;

}

Recompile the program using g++ like we did above and run the executable.


BAAAAM!!! It worked!!! you can see the content of the folder
'blabla' and your executable 'xyz.exe' is called from your recompiled program.

Explanation:

The new re-written program starts with '/' which is called root in UNIX/Linux based system.


In Windows system,
'C:\' have to replaced by '/' and write your path using FORWARD SLASH as shown in the re-written program "testExt.cpp".

Hope this may be helpful for you. Have a nice day!!! :)