Solved

Concatenating Char Array

Posted on 2001-06-15
6
390 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 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, " 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
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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 91
Socket Programming (Unix) 8 158
Safe conversion? 4 84
draw a Christmas tree by using a nested loop? 26 103
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

739 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