This is my program:
The program should behave like this:Code:char* arg_list[7]; for(int j=0; j<7; j++) arg_list[j] = new char(100); ................ string temp; vector<string> files; if(d) while(dir=readdir(d)) { temp = dir->d_name; if(temp.length()>=4) { temp = directory + temp; files.push_back(temp); } } ................ strcpy(arg_list[0],"gconftool-2"); strcpy(arg_list[1],"-t"); strcpy(arg_list[2],"string"); strcpy(arg_list[3],"-s"); strcpy(arg_list[4],"/desktop/gnome/background/picture_filename"); int child; while(1) { for(int i=0; i<files.size(); i++) { //this is the problem strcpy(arg_list[5],files[i].c_str()); arg_list[6] = NULL; strcpy(arg_list[4],"/desktop/gnome/background/picture_filename"); //end of this for(int k=0; k<6; k++) cout << arg_list[k] << endl; spawn("gconftool-2", arg_list); wait(&child); } }
gconftool-2
-t
string
-s
/desktop/gnome/background/picture_filename
/home/knight/wallpaper/grotto.jpg
But I got this behaviour:
gconftool-2
-t
string
-s
/desktop/gnome/background/picture_filename
ackground/picture_filename
If I comment this statement:
so that my source code like this:Code:strcpy(arg_list[4],"/desktop/gnome/background/picture_filename");
my program behave like this:Code://this is the problem strcpy(arg_list[5],files[i].c_str()); arg_list[6] = NULL; // strcpy(arg_list[4],"/desktop/gnome/background/picture_filename"); //end of this for(int k=0; k<6; k++) cout << arg_list[k] << endl;
gconftool-2
-t
string
-s
/desktop/gnome/b/home/knight/wallpaper/grotto.jpg
/home/knight/wallpaper/grotto.jpg
Could u explain this stupid behaviour of arg_list[4] and arg_list[5] that share the same memory?



LinkBack URL
About LinkBacks


