Re: passing a filename to main()
- From: "Erik Ohrnberger" <Erik echohome org>
- To: gtkmm-list gnome org
- Subject: Re: passing a filename to main()
- Date: Mon, 6 Mar 2006 11:30:48 -0500 (EST)
strdup would be another option i.e.
char* filename = strdup( argv[1 ] );
But don't forget to free the filename pointer when you are done with it.
On Mon, March 6, 2006 11:25, Leandro Fanzone wrote:
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
> </head>
> <body bgcolor="#ffffff" text="#000000">
> You can't copy a string without asigning space to it. So either you:<br>
> <br>
> char *inpname = argv[1];<br>
> printf(inpname);<br>
> <br>
> or<br>
> <br>
> printf(argv[1]);<br>
> <br>
> Otherwise you'll get a coredump. If you really want a copy of the
> string:<br>
> <br>
> char *inpname = new char[strlen(argv[1])+1];<br>
> strcpy(inpname,argv[1]);<br>
> printf(inpname);<br>
> <br>
> but as you're in C++, you can use STL strings:<br>
> <br>
> #include<string><br>
> <br>
> ...<br>
> <br>
> std::string inpname(argv[1]);<br>
> printf(inpname.c_str());<br>
> <br>
> Note that you should use printf("%s", inpname), or std::cout <<
> inpname.<br>
> <br>
> Mickael Drean wrote:
> <blockquote cite="mide4b61e4b0603060814t43ba35d1y mail gmail com"
> type="cite">Hi there,<br>
> <br>
> 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.<br>
> <br>
> Here is my code :<br>
> <br>
> int main(int argc, char *argv[])
> <br>
> {<br>
> <br>
> Gtk::Main kit(argc, argv);<br>
> <br>
> if (argc > 1)<br>
> {<br>
> char*
> inpname;<br>
>
> strcpy(inpname,argv[1]);<br>
>
> printf(inpname);<br>
> } <br>
> <br>
> CMyApp App;
> <br>
> Gtk::Main::run(App);<br>
> <br>
> return 0;<br>
> }<br>
> <br>
> <br>
> <pre wrap="">
> <hr size="4" width="90%">
> _______________________________________________
> gtkmm-list mailing list
> <a class="moz-txt-link-abbreviated"
> href="mailto:gtkmm-list gnome org">gtkmm-list gnome org</a>
> <a class="moz-txt-link-freetext"
> href="http://mail.gnome.org/mailman/listinfo/gtkmm-list">http://mail.gnome.org/mailman/listinfo/gtkmm-list</a>
> </pre>
> </blockquote>
> </body>
> </html>
> _______________________________________________
> 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]