Search code examples
ccommand-linefilenamesprintfinode

Save information from sprintf to a variable


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.


Solution

  • 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.