Concatenating Char Array

Posted on 2001-06-15
Last Modified: 2008-01-16
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}

Question by:dolphin203
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 32

Accepted Solution

jhance earned 30 total points
ID: 6196338
>>char p[50];  <<< What if the pathname is > 50?  Problem..

>>P[strlen(p)]= oldname; // this is where i get my error

What is "P"??  If you mean p, then this is wrong...

You should use the string functions like strcat() to concatenate strings.

Let's say you have:

char a[32];

// Put a string into a
strcpy(a, "This is a test");

// Concatenate to the end of it
strcat(a, "");

// OK, now see what's in a
printf("%s\n", a);

Will print...
This is a test


Expert Comment

ID: 6196406
>> 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';


Author Comment

ID: 6196821
ihance, are you indicating all I need do is;
// Concatenate to the end of it
...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.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 32

Expert Comment

ID: 6196839
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...

Author Comment

ID: 6196845
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.

Author Comment

ID: 6198775
Thanks jhance, strcat(p,oldfile) worked like a champ. I also used strcpy(p1,p) to create the directory path for; 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,

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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question