Ok so I am trying to write a piece of code that when a user inputs say "A = 'ls'" instead of outputting the files from the directory it will store the output in 'A'... If you get what i mean.

SO say the directory /Desktop has stuff.txt and main.cpp and i call "A = 'ls'" A would have "stuff.txt main.cpp" in it which i can then print later on.

I understand you can't just say A = execve(blah blah);
So how can it be done.

Here's what i have
cmd1 has the variable name in it and cmd2 has the 'ls' or 'ps' or whatever.

void variable_cmd(char** cmd1, char** cmd2)
{
	pid_t pid;
 	pid = fork();
	int i;
	if(pid == 0) 
	{
		i = execvp(argv[0], argv);
		if(i < 0) 
		{
			cout << argv[0] << " command not found" << endl;	
			exit(1);		
		}
	} 
	else 
	{
		wait(NULL);
	}

}

Thanks

Recommended Answers

All 4 Replies

You have several choices
1) open a pipe to the ls command
2) redirect the output of ls to a file, then open and read the file
3) generate the result yourself by using opendir() and readdir() standard C POSIX functions.

But you will most likely want to create an array of strings because ls might generate a lot of filenames.

Right...
Because both use exec() badly to call "ls" and various mentions of "capturing the output for later use"

No similarity at all.

But since you're spamming the board with duplicate questions, AND ignoring attempts to engage you in discussion about alternatives - good luck with that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.