0

is it possible to save information gather from a sprintf into a variable? The lines of code below are an example to better illustrate my question.

char fileName;
fileName = sprintf(command, "find -inum %i -type f", iNode);

The purpose is to find the file name associated with the inode number, then run "stat" on that file name.

seiryuu10
  • 843
  • 1
  • 10
  • 14
  • 2
    Can't you just use the `command` variable afterwards? – templatetypedef Feb 09 '13 at 05:19
  • I think you're looking for the [`popen`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/popen.html) function. – Some programmer dude Feb 09 '13 at 05:19
  • I am using the popen function in the overall program, but running into issues with it. Trying to see if there are other alternatives. – seiryuu10 Feb 09 '13 at 05:21
  • Then maybe you should post a question about those issues instead? All `sprintf` does is format a string, it doesn't run the program for you. – Some programmer dude Feb 09 '13 at 05:23
  • @templatetypedef can you be more specific in your answer? I am not sure what you mean by "use command variable afterwards". – seiryuu10 Feb 09 '13 at 05:23
  • @seiryuu10- I guess I don't understand your question. When you call `sprintf`, you produce a formatting string. It doesn't make any system calls. I don't get what you mean when you ask "save information gather from a sprintf into a variable," since `sprintf`'s entire point is to write a string to a variable. Can you be more specific about what you are trying to do? – templatetypedef Feb 09 '13 at 05:25
  • If I would venture a guess as to the problem with the `popen` call you apparently have, it's that the syntax of the `find` command is wrong. You forgot the _path_ from where to search. – Some programmer dude Feb 09 '13 at 05:46

1 Answers1

2

I think you want something like this:

FILE *fp;
char cmd[1024];
char filename[1024];

sprintf(cmd, "find -inum %i -type f", iNode);
fp = popen(cmd);
fgets(filename, sizeof filename, fp);
pclose(fp);

At the end of this code, filename will contain the fist line produced by the cmd.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73