?
Solved

Concatenating Char Array

Posted on 2001-06-15
6
Medium Priority
?
392 Views
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}
}

0
Comment
Question by:dolphin203
[X]
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
6 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 120 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, " 1.2.3.4");

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

Will print...
This is a test 1.2.3.4

OK?
0
 
LVL 7

Expert Comment

by:KangaRoo
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';

0
 

Author Comment

by:dolphin203
ID: 6196821
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.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Expert Comment

by:jhance
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...
0
 

Author Comment

by:dolphin203
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.
0
 

Author Comment

by:dolphin203
ID: 6198775
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.
0

Featured Post

Industry Leaders: 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 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