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

Started by
15 comments, last by KymikoLoco 11 years, 11 months ago
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.
Advertisement
You may want to push back a '\0' on the vector so the char array is '\0' terminated.
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

//load file_list into file_data with fread etc...
files[file_list] = file_data;


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.
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.
this is all rather confusing :( I'm having trouble making a idea of how to solve this from all the different answers..
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.
It seems as though no one in this thread is on the same page, but yes.

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() );


I hope this is closer to what you need.

This topic is closed to new replies.

Advertisement