Solved

Concatenating Char Array

Posted on 2001-06-15
6
376 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
  • 3
  • 2
6 Comments
 
LVL 32

Accepted Solution

by:
jhance earned 30 total points
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now