• Advertisement
Sign in to follow this  

How to convert a vector <char> to a const char?

This topic is 2179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[color=#000000][font=verdana, arial, helvetica, sans-serif]

Hello I'm having troubles doing conversions of vectors and chars sad.png.[/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

Heres my vector:[/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

vector<char> file_data; [/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

Heres my const char array :[/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

const char file_char;[/font]

[color=#000000][font=verdana, arial, helvetica, sans-serif]

How do I convert it??.[/font] Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
Advertisement
That doesn't make a lot of sense. A vector<char> contains potentially multiple characters. Why would you want to convert it to a single character?

Share this post


Link to post
Share on other sites
My wavefront .obj parser needs a const char *filename before it will parse the data as triangles on the screen. so conversion of the vector <char> to const char array is important in this case:


dir=opendir("Box Storage");
if(dir)
{
while(drnt = readdir(dir))
{
// printf("%-20s ", drnt->d_name);
file_list.push_back(drnt->d_name);
}

cout << "found the following files:\n";
for(int i = 0; i < file_list.size(); i++)
{
// cout << file_list << "\n";
}

map<string, vector<char> > files;
for(int i = 0; i < file_list.size(); i++)
{


//load file_list into file_data with fread etc...

files[file_list] = file_data;

}

//cout << "file data "<< &file_data[0] << endl;
//const char* file_char = &file_data[0];

//THIS WON'T WORK :)
loadObject(file_data[0]);


// loadObject(file_data);


}
Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
&file_data[0] will give you a pointer to the first element of the vector, which you should already know since it shows up in your commented out code.

Share this post


Link to post
Share on other sites
exactly thats why it won't work because its an element of the vector .

What I need is a filename from the const char array .

To do that I need to convert the vector with index 0 to a const char array so the filename which is contained within the const char array is the first file in the directory box storage.

My problem is how to do the above ^^.


Sorry if my responses are vague or weirdly constructured. Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
A vector is a dynamic array. A pointer to the first element of a vector is a pointer to an array of the type the vector stores. Pointers to a char array can be treated as a pointer to a const char array.

Share this post


Link to post
Share on other sites
then if thats the case why is my parser returning that it can't open up the filename? most likely because the vector doesn't contain the filename in which case how is that possible.? is it because I maped the data to memory because if so then that makes everything alot worser.


map<string, vector<char> > files;
Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement