-2

I have never used C++ before and my professor said this is supposed to be "very simple", but I can't figure it out. This is what I have so far:

#include <iostream>
#include <cstdio>
#include <cstdlib>

int main(int argc, char *argv[])
{
  char command[50];
  if (argc==2)
    system(command, "./findName.sh", argv[1]);
}  
return 0;

My shell script works when I run it by itself but I am not sure how to use a C++ program to run it. For the shell script, the user is supposed to enter a user ID like this:

./findName.sh userID

and the program returns the person's name from a file of names and user IDs like this:

LastName, FirstName

For the C++ program, it needs to pass the information the user enters to the shell script and return the same results.

As I said, I have never used C++ before so I don't know if any of this is right. It is a mix of things I have found online. Thank you for all of your help!!

msw
  • 42,753
  • 9
  • 87
  • 112
Abby.b
  • 1
  • 4
    `C++` is not really the kind of language you can cobble together bits you find laying around the internet. I suggest studying a *recommended* book methodically for a couple of hours a day. There are many bad books but these are recommended: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Galik Oct 09 '15 at 15:57

2 Answers2

0

The easiest way I find to do this is to use std::string as it allows you to concatenate stings easily.

#include <string>
#include <cstdlib>

int main(int argc, char* argv[])
{
    std::string command = "./findName.sh";

    if(argc == 2)
        std::system((command + " " + argv[1]).c_str());
}

But to understand why this works you are going to need to study some books.

First you need to compile the program before you can run it. If your source code file name is progname.cpp you should be able to use this command:

g++ -o progname progname.cpp

And then run the program like this:

./progname userID
Galik
  • 47,303
  • 4
  • 80
  • 117
  • Thank you very much! When I try to run it though it says : "syntax error near unexpected token '(' in 'int main ( int argc, char* argv[] )' ". Do you know why that is? – Abby.b Oct 09 '15 at 16:12
  • @Abby.b This code works for me, I just re-tested it. Did you input everything exactly as I have it? Its best to cut/n/paste, every comma, bracket and parenthesis has to be exactly right. – Galik Oct 09 '15 at 16:16
  • I copied and pasted but that is what it gave me. I just tried again in a new file and it did the same thing. – Abby.b Oct 09 '15 at 16:27
  • @Abby.b Did you compile the program or are you trying to run the source code directly? – Galik Oct 09 '15 at 16:35
  • @Abby.b I added instructions to compile the program just in case you are trying to run the source code. – Galik Oct 09 '15 at 16:39
  • @Abby.b Ok. The reason I checked is because i get the same error you describe if I try to run the source code. – Galik Oct 09 '15 at 16:40
  • That worked! Thank you so much!! I think the problem was when I tried to compile it I didn't put in the -o. THANK YOU!! – Abby.b Oct 09 '15 at 16:43
0

1) system("abc.sh " + para1 + " " + para2); But this method have a limitation, the max length you can pass into is MAX_ARG_STRLEN is defined as 131072 bytes=32 pages of memory.

2) open a file and read all the value to the file system("abc.sh < yourfile.txt"); This method is a bit indirect, but you can put unlimited size of value to sh inside the sh, use a normal read from buf ( or keyboard )