Save information from sprintf to a variable

孤人 提交于 2019-12-11 16:21:13

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/14785225/save-information-from-sprintf-to-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!