is this conversion safe?
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
-----------------------------------------------------------------------
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.
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.
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.
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.
-------------------------------------------------------------------------------
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?
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?
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
Cheers,
Twilight Dragon
That's not what youn are doing. Try using:
That should get the first line as the file name.
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.
Ultra,
If you want to, you can add something like these overloaded readline() members to your Input (ifstream extended) class:
These should read one line at the time into buffer, so you could do:
Then if you want, you can convert to a char by using my_string.c_str() where you like.
--random
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement