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

Started by
15 comments, last by KymikoLoco 11 years, 11 months ago
[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]

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?
oh a const char array smile.png so I can access it using operator [ ]
Vectors can already be accessed with operator [].
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);


}
&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.
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.
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.
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;
If you're storing a string, then why are you using std::vector instead of std::string?

This topic is closed to new replies.

Advertisement