Sign in to follow this  
DominicHughes

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

Recommended Posts

[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]Hello I'm having troubles doing conversions of vectors and chars [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img].[/size][/font][/color]

[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]Heres my vector:[/size][/font][/color]

[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3][CODE]vector<char> file_data; [/CODE][/size][/font][/color]

[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]Heres my const char array :[/size][/font][/color]

[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3][CODE]const char file_char;[/CODE][/size][/font][/color]

[color=#000000][font=verdana, arial, helvetica, sans-serif][size=3]How do I convert it??.[/size][/font][/color] Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
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
oh a const char array [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] so I can access it using operator [ ] Edited by DarkHorseKnight

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:

[CODE]
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[i] << "\n";
}

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


//load file_list[i] into file_data[i] with fread etc...

files[file_list[i]] = 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);


}
[/CODE] 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.

[CODE]
map<string, vector<char> > files;
[/CODE] Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
Likely candidates are because your vector doesn't contain the data you think it does, the working directory isn't what you think it is and/or you didn't null terminate the vector data. Without more information, it's impossible to say definitively what your problem is.

Share this post


Link to post
Share on other sites
What is file_data? A vector of chars? Why can't it hold strings to pass into the loadObject function? Does this not compile, or will it just not load the filename?

If it does compile, make sure the data in file_data is what you think it should be.

This part also doesn't make a whole lot of sense to me
[code]
//load file_list[i] into file_data[i] with fread etc...
files[file_list[i]] = file_data;
[/code]

You aren't loading file_list into file_data. If this is what you have, file_data has nothing inside it, because you aren't assigning things correctly.

Share this post


Link to post
Share on other sites
If loadObject() expects a file name as a parameter, why bother loading the file yourself?

However, most such APIs will provide a mechanism for performing the operation in memory. Look at the documentation for this library, discover if it handles this and use that mechanism for your manually loaded data. Obviously, this is more work for you so unless there is some benefit - e.g. storing all the files in a compressed archive - I would just use the handy "load a file contents directly" method.

Share this post


Link to post
Share on other sites
I could rewrite this directory reader then just get the filenames from the directory reader and store it in a vector to use as a filename with the [ ] operator?

because the vector is a dynamic array thankfully. Edited by DarkHorseKnight

Share this post


Link to post
Share on other sites
It seems as though no one in this thread is on the same page, but yes.
[code]
std::vector< std::string > file_name;
file_name.push_back( "C:\Dev\LOADME.obj" );
loadObject( file_name[0] );

// If the above needs a const char * parameter,
// and you don't want to change the definition:
loadObject( file_name[0].c_str() );
[/code]

I hope this is closer to what you need.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this