Find code

C

contactmayankjain

Hi,
can you please tell me what is wrong in this peace of code. Its not
giving any of the error at the compilation time nor its working as I
want it to do the things


#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* arcv[])
{

system("cd /");// Change the working directory
system("find ./ -name arcv[1]"); //find command
system("cd -"); // Goes back to the working directory
return 0;

}


Regards
Mayank Jain
www.mayankjain.110mb.com
 
A

Alf P. Steinbach

* (e-mail address removed):
Hi,
can you please tell me what is wrong in this peace of code. Its not
giving any of the error at the compilation time nor its working as I
want it to do the things


#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* arcv[])
{

system("cd /");// Change the working directory
system("find ./ -name arcv[1]"); //find command
system("cd -"); // Goes back to the working directory
return 0;

}

Well, formally it depends on your system, but in practice each system()
call runs its own separate command shell. Simply string the commands
into one biggie command. Exactly how you do that depends on your shell.

Oh, wait, that "arcv[1]" in there. C++ does not do variable
substitution inside strings (some script languages do).

Try this:

#include <vector>
#include <string>
#include <cstdlib>

int main( int argn, char* argv[] )
{
using namespace std;

vector<string> const arguments( argv, argv + argn );
string const cmd =
"cd /; find ./ -name " + arguments.at( 1 ) + "; cd -";

system( cmd.c_str() );
}

Disclaimer: off-the-cuff code.
 
C

contactmayankjain

* (e-mail address removed):


Hi,
can you please tell me what is wrong in this peace of code. Its not
giving any of the error at the compilation time nor its working as I
want it to do the things
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* arcv[])
{
system("cd /");// Change the working directory
system("find ./ -name arcv[1]"); //find command
system("cd -"); // Goes back to the working directory
return 0;

Well, formally it depends on your system, but in practice each system()
call runs its own separate command shell. Simply string the commands
into one biggie command. Exactly how you do that depends on your shell.

Oh, wait, that "arcv[1]" in there. C++ does not do variable
substitution inside strings (some script languages do).

Try this:

#include <vector>
#include <string>
#include <cstdlib>

int main( int argn, char* argv[] )
{
using namespace std;

vector<string> const arguments( argv, argv + argn );
string const cmd =
"cd /; find ./ -name " + arguments.at( 1 ) + "; cd -";

system( cmd.c_str() );
}

Disclaimer: off-the-cuff code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Hi,


You have done a good job.

Now I want these output to come with numbers in there beginning
like

1./usr/local/include/boost/mpl/aux_/test
2./usr/local/include/boost/test
3./usr/local/lib/ruby/1.8/rdoc/markup/test
4./usr/local/lib/ruby/1.8/test

now I want it to have such option so that I would be able to open the
file by just giving the option like vi 1 or vi 2 something like that,
and it got opened.

Can it be also possible to switch the user to a super user then back
to the normal user as in some of the case it gives an error message
Permission denied

find: ./usr/local/private: Permission denied



Regards
Mayank Jain
www.mayankjain.110mb.com
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

* (e-mail address removed):


Hi,
can you please tell me what is wrong in this peace of code. Its not
giving any of the error at the compilation time nor its working as I
want it to do the things
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char* arcv[])
{
system("cd /");// Change the working directory
system("find ./ -name arcv[1]"); //find command
system("cd -"); // Goes back to the working directory
return 0;

Well, formally it depends on your system, but in practice each system()
call runs its own separate command shell. Simply string the commands
into one biggie command. Exactly how you do that depends on your shell.

Oh, wait, that "arcv[1]" in there. C++ does not do variable
substitution inside strings (some script languages do).

Try this:

#include <vector>
#include <string>
#include <cstdlib>

int main( int argn, char* argv[] )
{
using namespace std;

vector<string> const arguments( argv, argv + argn );
string const cmd =
"cd /; find ./ -name " + arguments.at( 1 ) + "; cd -";

system( cmd.c_str() );
}

Disclaimer: off-the-cuff code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Hi,


You have done a good job.

Now I want these output to come with numbers in there beginning
like

1./usr/local/include/boost/mpl/aux_/test
2./usr/local/include/boost/test
3./usr/local/lib/ruby/1.8/rdoc/markup/test
4./usr/local/lib/ruby/1.8/test

When you use system() the C++ code does not get access to what the
commands you ran produced. Try piping through sed, awk, or some similar
language, perl or shell scripting might be a better choice than C++ for
what you are trying to do.
now I want it to have such option so that I would be able to open the
file by just giving the option like vi 1 or vi 2 something like that,
and it got opened.

Are you trying to build your own specialised shell or something? If
that's the case you probably should not be using external programs (like
find) to do your work, you have to do it yourself. Or look into fork()
and exec() and redirecting stdin/stdout, but such things are off-topic
in this group.
Can it be also possible to switch the user to a super user then back
to the normal user as in some of the case it gives an error message
Permission denied

find: ./usr/local/private: Permission denied

Again, off-topic, and generally a bad idea, if you want to run the
command as superuser do so, or use su/sudo, the program itself should
not be suid root.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,291
Messages
2,571,455
Members
48,132
Latest member
KatlynC08

Latest Threads

Top