Stupid C++ question. Command Line Arguments.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Ace12GA
Posts: 7
Joined: Mon Aug 08, 2005 2:43 am

Stupid C++ question. Command Line Arguments.

Post by Ace12GA »

So I am working on a project with irrlicht, and I am putting together a really basic binary for my mappers to run their maps in. Its basically just Tutorial 7 with some small changes. The change thats killing me is accepting 2 command line arguments to define the pk3 flie and the bsp within it. The idea is this:

maptest MyPak.pk3 MyMap.bsp

This would then cause irrlicht to load the pk3 file in a predefined directory, and then load the bsp specified.

So I altered the main() function declaration to:

Code: Select all

main(char *argv[])
Then inside the program I attempt to access the argv[1] and argv[2]. It compiles fine, but segmentation faults when it runs as soon as it hits an argv[] variable.

Code: Select all

char *mappackage;
sprintf (mappackage, "./maps/", "", argv[1]);
The code never gets past the argv, it just dumps. Don't hold me to the exact syntax, I hate C++, I am so a PHP programmer, its just really hard for me to drop back to C++ syntax, I am used to combing a string in php by just doing this:

Code: Select all

$mappackage = "./maps/".argv[1];
Not to mention the strict variable type declarations kill me; don't even get me started on pointers. ;) So whens that PHP binding for irrlicht going to be done?
Fred

Post by Fred »

Have you looked at Panda3D instead? It might be better for you - it's Python.

Anyway main should be something like:

Code: Select all

int main(int argc, char** argv)
{
}
Fred

Post by Fred »

A quick search through Google (found on the first page, which you REALLY should try BTW):

http://www.phon.ucl.ac.uk/courses/spsci ... sson11.htm

Code: Select all

#include <iostream>
#include <string>
using namespace std;
int main(int argc,char *argv[])
{
    cout << "argc=" << argc << endl;
    for (int i=1;i<argc;i++) 
        cout << "argv[" << i << "]=" 
             << argv[i] << endl;
    return(0);
}
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

more things...
you're allocating a pointer to a char (*mappackage) without pointing it at anything, and then filling it with data.
you should be creating an empty char array, like so-

Code: Select all

char mappackage[1024];
also you should learn the "printf" family of commands, your sprintf 'format' param has no symbols

Code: Select all

printf("param1 is char*: %s, param2 is float: %f", param1, param2);
Ace12GA
Posts: 7
Joined: Mon Aug 08, 2005 2:43 am

Post by Ace12GA »

Thanks for the replies guys, its all helpful. I did google btw, for about 20 minutes. I managed to get to about where I was.
Post Reply