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:
Anonymous2006-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:
Anonymous2006-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:
Anonymous2006-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:
Anonymous2006-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;
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:
Anonymous2006-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:
Anonymous2006-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;
# ...
>>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.
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:
Anonymous2006-06-14 5:46
>>20
Don't use a stupid language that doesn't support properties or something better.
>>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:
Anonymous2006-06-18 15:42 (sage)
Ban this motherfucker at once
and bring back Snacks!
Name:
Anonymous2006-06-21 3:30
Dammit moot, get back from Mexico and ban this shit
Name:
Anonymous2006-06-21 5:21
What is this?
I always get "account teminated". What account???
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