Objective-C, how to pass filenames to C function? -


i'm trying pass nsstring c function, although seems not accept (or ignore) parameters. appreciated - thanks.

int copyfiles(int argc, const char **argv) {     if(argc < 2 || argc > 3)     {         puts("usage: copy file [outfile]");         return 1;     }      const char *infile = argv[1];     char *outfile;     if(argc > 2)     {         outfile = strdup(argv[2]);         expect(outfile, "allocate");     } ... }  @implementation myapplication   @synthesize window;      - (void)copy:(nsstring *)pathtofile     {      nsstring *pathtofile = @"/path/to/file";      copyfiles((int)(const char *)[pathtofile utf8string],(const char **)[pathtofile utf8string]);     } 

i don't errors, output gives me "usage: copy file [outfile]", i'm not casting parameters correctly.

have @ call copyfiles, why you're passing string function wants integer first argument.

you need pass function argument count followed pointer-to-pointer argument list.

for example, call following c code (untested should general idea):

const char *args[] = {"copy", "fromfile", "tofile", null}; copyfiles (sizeof(args) / sizeof(*args) - 1, args); 

the first line creates array of character pointers (more correctly, c strings) including null @ end mandated iso c standard.

the second line passes 2 arguments, first being size of array minus 1 (the number of "real" arguments in list) , second being array itself.

in particular case, seem using one-filename variety, should start like:

char *args[3]; args[0] = "copy"; args[1] = [pathtofile utf8string]; // watch out auto-release here? args[2] = null; copyfiles (2, args); 

since c function expects main-like behaviour first argument "program" name.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -