?
Solved

Adding to char Arrays via loops

Posted on 2004-08-21
6
Medium Priority
?
254 Views
Last Modified: 2010-04-01
What I am trying to do is generate lists of files in folders and store them in a char array. However, I dont know how to add a filename on the next loop. How do you add things to a character array?

You cant do it like this:

char ch = new char[];

ch = "hi";

ch = ch + "hi"; // does not work

ch += "hi"; // does not work


I tried using strcat() but my program crashes whenver I use it.

Anyone?data2
0
Comment
Question by:sint4x
[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
  • 4
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 11862728
>>char ch = new char[];

The above should be the following:

char *ch = new char[123]; //Where 123 is the size of the array that you want
0
 
LVL 30

Expert Comment

by:Axter
ID: 11862736
char *ch = new char[123];

strcpy(ch, "Hello ");
strcat(ch, "World");

cout << ch << endl;
0
 
LVL 4

Author Comment

by:sint4x
ID: 11863891
Ahh, i see the only reason why it keeps crashing is because I didn't specify an array size.

Now how do I reset the size after I add things? (getting a directory list could be very large.)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Accepted Solution

by:
Axter earned 375 total points
ID: 11864915
>>Ahh, i see the only reason why it keeps crashing is because I didn't specify an array size.

Also, because it was not being assign to a pointer.


>>Now how do I reset the size after I add things? (getting a directory list could be very large.)

I recommend you use a std::vector and an std::string for this requirement.
Example:

#include <vector>
#include <string>
#include <iostream>

std::vector<std::string> MyArrayOfFileNames;

//To add a name
MyArrayOfFileNames.push_back("SomeFileName");

//To referance a name

std::cout << MyArrayOfFileNames[0] << std::endl;

0
 
LVL 30

Expert Comment

by:Axter
ID: 11864926
>>Now how do I reset the size after I add things? (getting a directory list could be very large.)

Otherwise, you would need to create an array of pointer, which would require that you have an idea of the quantity of file names you're going to store in advance.

Then you would have to assign space for each file name, and set it to an each array pointer item.
0
 
LVL 3

Expert Comment

by:CmdrRickHunter
ID: 11876027
do you understand pointers?  If not, then this is going to be some black magic:

char* data = new char[256],
      * p = data; // "p" moves along in the "data" array
const char[] filename1 = "hello\n";
const char[] filename2 = "world\n";
strcpy(p, filename1); // put "hello\n" into p (into the data array)
p += strlen(filename1); // this is the pointer magic
strcpy(p, filename2);
p += strlen(filename2);


what the "p += strlen(theStringJustCopied)" does is move p forward in memory, so that it points to the character one after the last character you added.

just remember, even though p is a char*, dont delete it!!  All its doing is pointing to memory that got allocated as part of "data".  DELETE "data" NOT "p"!!!


if you really want to:

void AddToBuffer(char*& inP, const char* inNewString)  // if this doesn't work, try char &*
{
   strcpy(inP, inNewString);
   inP += strlen(inNewString);
}


then you can do
char* data = new char[1250];
char * p = data;
AddTobuffer(p, "Hello ");
AddToBuffer(p, "world");


the trick to that working is the "pass pointer by reference", which passes a char*, but does so by reference so that we can change what the pointer is pointing AT.
0

Featured Post

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.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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 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.
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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