User input in a system("wget")

Mar 15, 2008 at 11:08pm
Currently I'm attempting to get a webpage that the user inputs using a system call. However I can't get the system call with the user's inputted website working.

My program looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
 #include <iostream>
  #include <string>
  int system(const char *command);

    int main()
   {
      string website;
      cout<<"Enter a website that you want to grab: ";
      cin>>website;
  
      int get=system ("wget "+website);
      return get; 
   }



Desired output:
Enter a website that you want to grab: www.yahoo.com

With the index.html of yahoo put in the directory the program is being run in.


Any help would be greatly appreciated.
Last edited on Mar 15, 2008 at 11:09pm
Mar 17, 2008 at 2:08pm
What is the purpose of this line?
 
int system(const char *command);


Tell me more about your problem. Is it not compiling? If it compiled correctly, what happens when you run it? Does it crash, hang, or just display nothing?
Mar 17, 2008 at 2:21pm
It seems as though system() won't accept a string as an argument- you must supply a pointer to a const char. That's fine- we'll use c_str() to convert the string to a c-style string, and then cast it into a const char*.

1
2
3
4
5
6
7
8
int main()
{
	std::string website;
	std::cout<<"Enter a website that you want to grab: ";
	std::cin>>website;
	std::string command = "wget " + website;
	system((const char*)command.c_str());
}


Don't forget your "std::"s, also.
Mar 18, 2008 at 8:41pm
There's no reason to cast command.c_str() to a const char*, it already of that type, this would be fine:

 
system(command.c_str());


Also, you should not declare system(const char*) yourself, but include <cstdlib> instead.
Topic archived. No new replies allowed.