Link to home
Start Free TrialLog in
Avatar of dolphin203
dolphin203

asked on

Concatenating Char Array

I'm writing a routine to rename files in a folder. To do this I'm providing the user an interface to select the path, oldfilename, and newfilename. The program works fine if I require the entire path to be entered. However, if I allow the user to provide a path...c:\ctest\ and then ask for the old and new file...test.txt prog.txt I am having trouble concatenating the path and file to provide...c:\ctest\test.txt as a char array. Here's example of code.
What I ultimately want to achieve is to have program run and change the first character of all files in the folder on the fly. Any help would be appreciated. I looked at using the CString class but could not get it to work either.
int main()
{
int result;
char p[50];
char oldname[30];
char newname[30];

cout << "Enter path as c:\\path\\" <<endl;
cin >> p;
cout << "Enter old file ie test.txt. \n";
cin >>oldname;
P[strlen(p)]= oldname; // this is where i get my error
cout << "Enter new file ie prog.txt. \n";
cin >>newname;
P[strlen(p)]= newname; // this is where i get my error
result = rename(newname,oldname);
if(result !=0)
{
error routine here
}
return 0}
}

ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KangaRoo
KangaRoo

>> change the first character of all files
in the folder on the fly

That can be solved a bit different then by strcpy and strcat. You can change 'any' character in a char array[]:

char astring[] = "Hullo world";
astring[1] = 'e';

Avatar of dolphin203

ASKER

ihance, are you indicating all I need do is;
// Concatenate to the end of it
strcat(p,oldname);
...and p would have the path and file? I'll try it and get back to you. What header file does strcat fall under...That's ok, I'll look it up. Will get back to you in a couple hours...have a doc appt.

Yes, that's what I'm saying.  But I'm also saying that you need to be sure that p is large enough to hold the resultant string.  You have made space for only 50 characters.  Quite a small amount for a file pathname...
kangaRoo, I'm aware of how that's done. What I'm looking for is how to read the file names from a Windows folder into a char array one at a time...then--yes I would use the array pointer to change the first character. I envision it working something like this;

1. A "while" or "for" loop--but what terminates the number of files in a folder and how are they inputted? It's not like reading in data from a file. That I would not have a problem with. I've never worked with the Windows shell commands using C++, to actually do a form of "Dir" to list the files.
Thanks jhance, strcat(p,oldfile) worked like a champ. I also used strcpy(p1,p) to create the directory path for newfile...ie...; strcat(p1,newfile). I used a while loop and a flag so as I might step thru the folder renaming other files. By the way I increased the array sizes to 81. For my purpose, I really didn't need anymore than 30--but who knows what the future might bring. Do you have any ideas on the second part of my question? "Is there a way to cycle thru all the files in a folder to end of folder"...perhaps the windows.h header might provide a DOS command I could use. Does it have a DIR method that can be read and changed?
Thanks again,
dolphin203

PS...Thanks also to KangaRoo for his response. I'm sure it will be helpful to anyone reading this problem that has little experience with arrays.