hello there, i have here a program(linux based) that creates a directory then transfer files to that said directory...the problem is, im using strcat to join my bash script commands to the users inputs..now, i have a loop that concatenates the commands...
is there a way that a concatenation will refresh in a loop...here's my program...

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <cstring>
 
using namespace std;
 
int main(){
 
char filename[50];
char directory_name[50];
char destination[50]= "cp ";
char mkdir_comm[50]= "mkdir ";
string destination_storage[50];
int number_of_files;
 
cout << "Enter directory name: ";
cin >> directory_name;
cout << "\n";
 
strcat ( mkdir_comm, directory_name);
 
(const char)*mkdir_comm;
 
system(mkdir_comm);
 
cout << "Directory successfully created: \n";
cout << "\n";
 
system("ls -l");
 
cout << "How many files do you want to transfer?:\n ";
cin >> number_of_files;
 
for(int i=0; i<number_of_files; i++){
cout << "Enter filename " << i+1 << ": ";
cin >> filename;
 
strcat( destination, filename );
strcat( destination, " ");
strcat( destination, directory_name);
 
(const char)*destination;
 
system(destination);
}
 
 
}

the cp command will ignore the commands on the 2nd loop because the concatenation does not refresh...help pls...

50 characters for a directory name isn't enough space. What if I copy/paste a path that is more than 50 characters? You need at least 255 characters.

line 23: that does nothing. Delete that line.

line 43: ditto -- nothing more than a do-nothing line.

Now for your question: On line 38 you need to re-initilize the variable destination with the text "cp ". just use strcpy() for that strcpy(destination,"cp "); I think that will fix the problem.

thanks dude...problem solved..and oh, i followed all your advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.