Thread: system() problem

  1. #1
    Registered User
    Join Date
    Sep 2005

    system() problem

    Hello All;

    I am almost done...

    Below is my code. notice how I build toDo. I need this built string to be executed by system(). I will not compile. Here is the code and error:

    // test-app.cpp : Defines the entry point for the console application.
    #include "stdafx.h"
    #include <ctime>
    #include "string.h"
    #include <iostream>
    #include <string>
    using namespace std;
    int _tmain(int argc, _TCHAR* argv[])
    	//system("c:\progra~1\PostgreSQL\8.1\bin\pg_dump focus > \\\smbtes\backup.backup");
    	string filename;
    	char dateStr[9];
    	string toDo;
    	filename = "Backup-";
    	filename = filename + dateStr;
    	filename = filename + ".backup";
    	toDo = "pg_dump -h localhost -U swanley007 focus > \\\\\\smbtest\\";
    	toDo = toDo + filename;
        //cout << filename << endl;
    	//cout << toDo << endl;
    	int i;
    	puts ("Trying to execute command PG_DUMP");
    	i= system (toDo);
    	//i= system ("pg_dump -h localhost -U swanley007 focus > \\\\\\smbtest\\backup.backup");
    	//i = system ("pg_dump focus > \\\smbtest\backup.backup");
    	system ("pause"); 
    	return 0;
    c:\documents and settings\stan swank ii\my documents\visual studio 2005\projects\test-app\test-app\test-app.cpp(32) : error C2664: 'system' : cannot convert parameter 1 from 'std::string' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Build log was saved at

    Thanks for any help

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    >> i= system (toDo);

    Does system return something?

    system ( toDo.cstr() );

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Yes, it returns the exit code of the called command.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2006
    use toDo.c_str() instead of toDo.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > Yes, it returns the exit code of the called command.
    But what is the called command?

    Many DOS/Windows implementations of system() always return 0, because what actually gets run when you do
    system( "foo.exe" );
    cmd /c foo.exe
    and what you get back is the exit status of cmd.exe, not the exit status of foo.exe.

    Try it with a simple sub-program which is just
    int main ( ) { return 1; }
    And see what result gets assigned when you invoke it with system().

    If you want more detailed information, then try the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem while constructing IP packet and sending using socket() system call
    By cavestine in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-15-2007, 05:49 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem with system(), execl(), etc. functions
    By nickkrym in forum C Programming
    Replies: 5
    Last Post: 05-12-2007, 08:04 AM
  4. problem installing TURBOC in my system
    By ss12 in forum C++ Programming
    Replies: 3
    Last Post: 03-18-2005, 08:57 AM
  5. Another problem swith my wide system MouseProc
    By darcome in forum Windows Programming
    Replies: 0
    Last Post: 10-03-2002, 12:34 PM