system() problem

This is a discussion on system() problem within the C++ Programming forums, part of the General Programming Boards category; Hello All; I am almost done... Below is my code. notice how I build toDo. I need this built string ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    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:

    Code:
    // 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 > \\10.1.65.30\smbtes\backup.backup");
    	string filename;
    	char dateStr[9];
    	string toDo;
    	//_strdate(dateStr);
    	_strdate_s(dateStr);
    	
    
    	filename = "Backup-";
    	filename = filename + dateStr;
    	filename = filename + ".backup";
    	toDo = "pg_dump -h localhost -U swanley007 focus > \\\\10.1.2.16\\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 > \\\\10.1.65.30\\smbtest\\backup.backup");
    	//i = system ("pg_dump focus > \\10.1.65.30\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
    Location
    Ireland
    Posts
    2,262
    >> i= system (toDo);

    Does system return something?

    system ( toDo.cstr() );

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

    "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
    Posts
    903
    use toDo.c_str() instead of toDo.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    > 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" );
    is
    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
    Code:
    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
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21