linux - storing directory size into integer variable using c++ -
below c++ code in trying store size of directory integer variable
char path[60]; char exec[180]; sprintf(path,"%s","/home/directory"); sprintf(exec,"du %s",path); int k; k = system(exec); printf("\n value = %d\n",k);
and output
556 /home/directory value = 0
it not storing in k
here can store output file using either
sprintf(exec,"du %s" > file.txt,path); sprintf(exec,"du %s >> file.txt",path);
but again have open file(file.txt) , read data variable.
my question there other alternative store size of directory integer variable kindly suggest me there alternative in advance
as joachim & mats explained, this.
below code reference only!
#include <stdio.h> const int max_buffer = 2048; char path[60]; char cmd[180]; sprintf(path,"%s","/home/directory"); sprintf(cmd,"du /home/directory",path); char buffer[max_buffer]; file *stream = popen(cmd, "r"); if (stream) { while (!feof(stream)) { if (fgets(buffer, max_buffer, stream) != null) { //... } } pclose(stream); }
Comments
Post a Comment