Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 May 2012 - 04:10 PM

Hello I'm having troubles doing conversions of vectors and chars Posted Image.

Heres my vector:

vector<char> file_data;


Heres my const char array :

const char file_char;


How do I convert it??.

Edited by DarkHorseKnight, 01 May 2012 - 04:16 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 01 May 2012 - 04:11 PM

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?

#3 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 May 2012 - 04:15 PM

oh a const char array Posted Image so I can access it using operator [ ]

Edited by DarkHorseKnight, 01 May 2012 - 04:17 PM.


#4 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 01 May 2012 - 04:23 PM

Vectors can already be accessed with operator [].

#5 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 May 2012 - 04:26 PM

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[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 <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/smile.png' class='bbc_emoticon' alt=':)' />
loadObject(file_data[0]);
  
  
  //  loadObject(file_data);
  
			  
		}

Edited by DarkHorseKnight, 01 May 2012 - 04:28 PM.


#6 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 01 May 2012 - 04:31 PM

&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.

#7 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 May 2012 - 04:34 PM

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, 01 May 2012 - 04:35 PM.


#8 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 01 May 2012 - 04:41 PM

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.

#9 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 May 2012 - 04:44 PM

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, 01 May 2012 - 04:45 PM.


#10 Oberon_Command   Crossbones+   -  Reputation: 1899

Like
0Likes
Like

Posted 01 May 2012 - 04:46 PM

If you're storing a string, then why are you using std::vector instead of std::string?

#11 SiCrane   Moderators   -  Reputation: 9597

Like
0Likes
Like

Posted 01 May 2012 - 04:47 PM

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.

#12 bradbobak   Members   -  Reputation: 1100

Like
0Likes
Like

Posted 01 May 2012 - 04:48 PM

You may want to push back a '\0' on the vector so the char array is '\0' terminated.

#13 KymikoLoco   Members   -  Reputation: 195

Like
0Likes
Like

Posted 01 May 2012 - 04:58 PM

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[i] into file_data[i] with fread etc...
                files[file_list[i]] = 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.

#14 rip-off   Moderators   -  Reputation: 8222

Like
0Likes
Like

Posted 01 May 2012 - 05:16 PM

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.

#15 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 May 2012 - 05:16 PM

this is all rather confusing :( I'm having trouble making a idea of how to solve this from all the different answers..

#16 DarkHorseKnight   Members   -  Reputation: 143

Like
0Likes
Like

Posted 01 May 2012 - 05:20 PM

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, 01 May 2012 - 05:20 PM.


#17 KymikoLoco   Members   -  Reputation: 195

Like
1Likes
Like

Posted 02 May 2012 - 07:34 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS