Re: passing a filename to main()



You can't copy a string without asigning space to it. So either you:

char *inpname = argv[1];
printf(inpname);

or

printf(argv[1]);

Otherwise you'll get a coredump. If you really want a copy of the string:

char *inpname = new char[strlen(argv[1])+1];
strcpy(inpname,argv[1]);
printf(inpname);

but as you're in C++, you can use STL strings:

#include<string>

...

std::string inpname(argv[1]);
printf(inpname.c_str());

Note that you should use printf("%s", inpname), or std::cout << inpname.

Mickael Drean wrote:
Hi there,

 I tried to specify a file to load to my application on startup but it didn't works. At that moment I tried to only show the name of the file specified.

 Here is my code :

int main(int argc, char *argv[])
{
   
    Gtk::Main kit(argc, argv);
   
    if (argc > 1)
    {
             char* inpname;
             strcpy(inpname,argv[1]);
             printf(inpname);
    }
   
    CMyApp App;
    Gtk::Main::run(App);
   
    return 0;
}



_______________________________________________ gtkmm-list mailing list gtkmm-list gnome org http://mail.gnome.org/mailman/listinfo/gtkmm-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]