Jump to content
  • Advertisement
Sign in to follow this  
Ultra The Vampire

is this conversion safe?

This topic is 4853 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

the following code compiles ok ----------------------------------------------------------------------- const irr::c8* PD_Entity_Loader::Get_Entity_Mesh_Path() { ifstream Entity_Definition_File; Entity_Definition_File.open("Entity_List.txt"); char buffer[512]; Entity_Definition_File.getline(buffer, 512); //get the first line in the buffer Entity_Definition_File.close(); irr::c8* temp; Entity_Definition_File >> temp; return (temp); } ----------------------------------------------------------------------- but then i get following warning. Is if the coding safe to use without losing the value of temp and returning it safely or not? if not can someone help me find a different method of converting ASCII text to a const char and returning it safely. Just so you know irr::c8 is a typedef for char. ----------------------------------------------------------------------- c:\coding\irrlicht\ice\ice_3.0\ice_3\src\demo\gamestates\gs_demofakegame.cpp(52) : warning C4700: local variable 'temp' used without having been initialized -----------------------------------------------------------------------

Share this post


Link to post
Share on other sites
Advertisement
That code is non-kosher on several levels. You seem to be using the extraction operator to fill a char pointer. Problem 1: you closed the file so extraction isn't going to work. Problem 2: you're filling a character pointer that doesn't have any storage. If that extraction worked, and fortunately it doesn't, you'd probably generate an seg fault as you wrote to random memory. This is bad. Returning char *s from a function in general is bad news. Consider using a std::string instead.

Share this post


Link to post
Share on other sites
Technically streaming into the variable initializes it. However, it's still going explode. First you create a file, read a line from it, then close it. You then proceed to stream from this closed file into a pointer. Assuming you have an overloaded operator>> for that pointer type, it streams into memory who knows where. What's the point of the char array? You never use it. You never created the new entity, but you tried to stream into it anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ultra The Vampire
problem is, i need to return const char, because of the API i am using, the function i am using this function in requires a const char value to be valid.


Then make the class you're writing store the std::string as a member variable and return the c_str() in your function.

Share this post


Link to post
Share on other sites
-------------------------------------------------------------------------------
const irr::c8* PD_Entity_Loader::Get_Entity_Mesh_Path()
{
ifstream Entity_Definition_File;
Entity_Definition_File.open("Entity_List.txt");

char buffer[512];
Entity_Definition_File.getline(buffer, 512); //get the first line in the buffer

Entity_Definition_File >> Entity_Path_To_Mesh;

Entity_Definition_File.close();

return (Entity_Path_To_Mesh.c_str());
}
-------------------------------------------------------------------------------

ok, this compiles fine with no warnings or errors.
I should'nt lose data right?

Share this post


Link to post
Share on other sites
You'll the first line's worth of data that you stored in buffer; of course that may be your intent (but if it is, ignore() is cleaner). The rest of the data from the file will end up in the string you return.

Cheers,
Twilight Dragon

Share this post


Link to post
Share on other sites
That's not what youn are doing. Try using:

ifstream Entity_Definition_File("Entity_List.txt");
std::getline(Entity_Definition_File, Entity_Path_To_Mesh);
return Entity_Path_To_Mesh.c_str();

That should get the first line as the file name.

Share this post


Link to post
Share on other sites
Ultra,

If you want to, you can add something like these overloaded readline() members to your Input (ifstream extended) class:


void Input::readline(string & str)
{
std::getline(*this, str);
}

string Input::readline()
{
string str;
readline(str);
return str;
}





These should read one line at the time into buffer, so you could do:


Input in(my_filename);
in.skiplines(2) // to skip your header.
string my_string = in.readline(); // to read one line as string.





Then if you want, you can convert to a char by using my_string.c_str() where you like.

--random

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!