Re: passing a filename to main()



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&lt;string&gt;<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 &lt;&lt;
> inpname.<br>
> <br>
> Mickael Drean wrote:
> <blockquote cite="mide4b61e4b0603060814t43ba35d1y mail gmail com"
>  type="cite">Hi there,<br>
>   <br>
> &nbsp;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>
> &nbsp;Here is my code :<br>
>   <br>
> int main(int argc, char *argv[])
>   <br>
> {<br>
> &nbsp;&nbsp;&nbsp; <br>
> &nbsp;&nbsp;&nbsp; Gtk::Main kit(argc, argv);<br>
> &nbsp;&nbsp;&nbsp; <br>
> &nbsp;&nbsp;&nbsp; if (argc &gt; 1)<br>
> &nbsp;&nbsp;&nbsp; {<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; char*
> inpname;<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
> strcpy(inpname,argv[1]);<br>
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
> printf(inpname);<br>
> &nbsp;&nbsp;&nbsp; } <br>
> &nbsp;&nbsp;&nbsp; <br>
> &nbsp;&nbsp;&nbsp; CMyApp App;
>   <br>
> &nbsp;&nbsp;&nbsp; Gtk::Main::run(App);<br>
> &nbsp;&nbsp;&nbsp; <br>
> &nbsp;&nbsp;&nbsp; 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]