Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

New to C(++): How do I used cmd-line params?

Name: Anonymous 2006-05-11 11:01

Hay guys!
I wated to program a little program that accepts parameters from the command-line. So I want to open it like

C:\whatever.exe param1 param2 ... paramn

So I wrote a bit.



#include <iostream>
#include <string>
using namespace std;
int main (int argc, string * argv[])
{
  int i;
  string ID;
  string action;
  string mode;
  string file;
  string real_file_name;
  int ret_val;
  ID             = argv[1];
  action         = argv[0];
  mode           = argv[2];
  file           = argv[2];
  real_file_name = argv[3];
//CODE
  return ret_val
}


But when I compile it, there are lots of errors... and they have something to do how I initialize those variables. (something with conversion from string * to basic_string)
argc works fine, but I expected it to :)

What's the problem here? Please explain it a bit or give me a good online-source where I can read this up!

And if you are asking: I'm using the command-line copiler from Microsoft... dunno if that'S important for you, though...

Name: Anonymous 2006-05-11 12:11

This is Microsoft, so I don't know what they are upto in these days, but shouldn't you be using char arrays(char* argv[] for example) instead of C++ strings ?

Name: Anonymous 2006-05-11 12:51

>>2
I should do this, but I find it very uncomfortable to work with char-arrays :)
how can I compare them and how can I assign them?

like this: char character[20] = "Whatever"
or like this:
for (i=1;i<[lengh of text];i++){
  chararray1[i] = anotherchararray[i]
}

Problem with the first one: They must have a variable length.
Problem with the second one: Well, I guess you can see it...

Name: Anonymous 2006-05-11 13:06

string.h has the stuff you need - but you can also do without,
as you can convert your char arrays, integers, floating point numbers etc into strings via a stringstream (look at <sstream>) ASAP. But first you need to get your commandline arguments in an array of char arrays and then convert them to C++ strings for later manipulation

Name: Anonymous 2006-05-11 14:11

Thanks for your effort, anonymous, but I found a solution (how I'm home and have my C++Builder :) )


#include <iostream.h>

int main( int argc, char * argv[] )
{
  int ret_val;
  string action;
  string ID;
  string filename;
  string real_filename;
  string mode;

  ret_val = 1;
  action = argv[1];
  ID = argv[2];
  filename = argv[3];
  real_filename = argv[4];
  mode = argv[3];

  if (argc > 1) {
    if (action == "put") {
      // todo: enter functions for put
      ret_val = 0;
    } else if (action == "get") {
      // todo: enter functions for get
      ret_val = 0;
    } else if (action == "getOL") {
      // todo: enter functions for getOL
      ret_val = 0;
    } else if (action == "view") {
      // todo: enter functions for view
      ret_val = 0;
    } else if (action == "delete") {
      // todo: enter functions for delete
      ret_val = 0;
    } else {
      cout << "unacceptable parameter!";
      ret_val = 1;
    }
  }
  return ret_val;
}

Name: Anonymous 2006-05-11 14:19

>>5
oops :)
I should initialize some of the variables (like mode) later in the program or just check some things first (as you may have guessed: I don't need all of them everytime)

How it works just as it's supposed to :)

Name: Anonymous 2006-05-11 20:26

>>5
Why are you using C++ for such a task? Wouldn't Perl be simplier in this case:
my ($action, $id, $filename, $realname, $mode) = @ARGV;
# ...

Name: Anonymous 2006-05-12 2:51

>>7
Why didn't I use Pascal?

begin
  i := ParamCount;
  LastParam := ParamStr(i)
end.

You can do all those tasks with nearly every language.

Name: Anonymous 2006-05-12 10:38

>>8
yes you can, but in SOME languages (Perl, wink wink), it's "eaaaaaaaasier."

Name: Anonymous 2006-05-17 11:17 (sage)

>>9
if you find c++'s way hard then GTFO.

Name: Anonymous 2006-06-13 7:09

>>1
string ID;
string action;
string mode;
string file;
string real_file_name;
Make this a class.

Name: Anonymous 2006-06-13 7:27

>>12
why?

Name: Anonymous 2006-06-13 7:45

>>13
Because >>12 is a JAVA programmer and if it isn't overengineered object oriented SOAP on an XML rope it's not Enterprise.

Seriously if this were a large program you'd probably want to factor that stuff into a class, but for a toy program you've got better things to do with your time.

Name: Anonymous 2006-06-13 9:30

>>12
You fail

Name: Anonymous 2006-06-13 9:54

Oh come on, it was the perfect time to say something like that.

Name: Anonymous 2006-06-13 10:06 (sage)

>>12,16
Same person.
ENTERPRISE ENTERPRISE LOL

>:(

Name: Anonymous 2006-06-13 10:42

:P >>12,16,18  Me. And I don't write Java.

Name: Anonymous 2006-06-13 17:50

>>12
good idea

>>14
moron

Name: Anonymous 2006-06-14 3:43

Lol OO

class harbl {
  private int i;
  public int getI() { return i; }
  public void setI(int x) { i = x; }
}

Now this is much better than int i because it's enteprirse and it scales to deliver high-performance Web 2.0 applications.

Name: Anonymous 2006-06-14 5:46

>>20
Don't use a stupid language that doesn't support properties or something better.

Name: Anonymous 2006-06-14 6:24

It's funny because >>21 didn't get it.

Name: Anonymous 2006-06-14 9:23

>>22
and it seems you didn't get it too :D

Name: Anonymous 2006-06-14 9:35 (sage)

>>23
What?

Name: Anonymous 2006-06-14 19:29

>>24
Again? :D

Name: Anonymous 2006-06-14 20:02

for cstring use null terminated arrays and then use
strcmp(&a[0],&b[0]);
or
strcmp(&a[0],"foobar");
returns a bool value.

Name: Anonymous 2006-06-15 9:07

Actually, strcmp returns 0 if the strings are equal.

Name: Anonymous 2006-06-15 9:19

>>27
Which, in C/C++, is TRUE.

Name: Anonymous 2006-06-15 10:01

>>28
Er. 0 = false, 1 = true.

Name: Anonymous 2006-06-15 10:05

>>29
Ah yeah, I remember :)
When the returned errorcode of a program is 0 then there was no error (which can be expressed as TRUE).

Name: Anonymous 2006-06-15 10:13

>>30
Yeah, you gotta love Unix consistency :)

Name: Anonymous 2006-06-15 11:42

>>29
Incorrect!

#define false 0
#define true !false

Name: Anonymous 2006-06-15 12:36

>>27
ah, sorry my misstake, i wrote that being half asleep.
strcmp dosen't return a bool value, it returns 0 (as >>27 said) if the strings have no difference.
I think it returns -X if any letter in the string is lesser than the one compared to, and X if it's greater.

Name: Anonymous 2006-06-18 15:42 (sage)

Ban this motherfucker at once
and bring back Snacks!

Name: Anonymous 2006-06-21 3:30

Dammit moot, get back from Mexico and ban this shit

Name: Anonymous 2006-06-21 5:21

What is this?
I always get "account teminated". What account???

Name: Anonymous 2006-06-21 6:21

The account of that retard's web host maybe

Name: Anonymous 2006-07-04 5:04 (sage)

>>33

And what if X itself is negative?

Name: Anonymous 2010-06-07 6:45

Hi, I can spam /prog/ too, you faggot.

Also, smoke weed everyday.

Name: Sgt.Kabu芕kiman쩖뒲 2012-05-28 18:53

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List