C++ arguments problem

This is a discussion on C++ arguments problem within the C++ Programming forums, part of the General Programming Boards category; Hello all; I am running windows...developing a windows command program. I am having some problems with my command line arguments ...

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

    C++ arguments problem

    Hello all;

    I am running windows...developing a windows command program.

    I am having some problems with my command line arguments though.

    Here is my code:
    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, char* argv[])
    {
    	int j;
    	//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);
    	
    	for (j = 0; j < argc; j++)
    	{
    		cout << "Argc: " << j << " = " << argv[j] << endl;
    	}
    
    	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.c_str());
    	//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;
    
    
    }


    here is my run and output:

    C:\Program Files\PostgreSQL\8.1\bin>test-app -h
    Argc: 0 = t
    Argc: 1 = -
    Trying to execute command PG_DUMP
    Access is denied.
    Press any key to continue . . .

    C:\Program Files\PostgreSQL\8.1\bin>test-app -h 192.168.1.1
    Argc: 0 = t
    Argc: 1 = -
    Argc: 2 = 1
    Trying to execute command PG_DUMP
    Access is denied.
    Press any key to continue . . .

    C:\Program Files\PostgreSQL\8.1\bin>



    I am wondering why it is only getting the first character in each argument. I think is has to do with the char*, but this works in linux fine..... Help would be great

  2. #2
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    maybe use getline with the arguments? Don't use string.h! Use

    Code:
    #include <string>
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    I can't seem to get this to work

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I can't seem to get this to work
    Details, please. What OS and compiler? What settings are you using? If you break the program down to just printing argv, does the same problem occur? Chip away everything unnecessary until you have the smallest possible program that exhibits the problem. Then post it.
    My best code is written with the delete key.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Strange! It works fine in my Windows XP.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    I think the problem is in my use of getline...can you give me the correct syntex to test your theory. My c++ is a little rusty!

    Thanks

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I think the problem is in my use of getline...
    You're not using getline. If your code has changed that much, please repost it so that we can see your most current problem. Also, getline has nothing to do with command line arguments. getline reads input from a stream, but command line arguments are already present in argv at program startup. The two mechanisms are mutually exclusive in this case.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    My output remains the same, and i have commentted everything out but the command line stuff...here is the code:

    Code:
    // test-app.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <ctime>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int _tmain(int argc, char* argv[])
    {
    	int j;
    	//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);
    	
    
    	for (j = 0; j < argc; j++)
    	{
    		cout << "Argc: " << j << " = " << argv[j] << endl;
    	}
    
    	//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.c_str());
    	//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;
    
    
    }

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Figured it out!!!!!!!!!!

    _tmain should be just plain main

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Next time you should post what your errors are, if there are any. Posting this
    Code:
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.text+0x18): In function `_start':
    ../sysdeps/i386/elf/start.S:98: undefined reference to `main'
    collect2: ld returned 1 exit status
    would have gotten your answer for you a lot sooner.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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