Jump to content
  • Advertisement
Sign in to follow this  
savail

Loading data from txt file to LPCSTR variable

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

Hey,
I need to load paths to particular sounds from txt file and pass those paths to vector of LPCSTR type. Therefore I have some questions about the code I've made:
std::vector <LPCSTR> waveFile;
std::fstream file( "data/sounds.txt", std::ios::in );
char line[255];
while( !file.eof() )
{
file.getline( line, 255, ' ' );
file.getline( line, 255, '\n' );
waveFile.push_back(line);
}
file.close();

This code doesn't work... I have no certain idea why but out of while loop every waveFile element has the same path (the last one loaded from txt)...

I came up with a solution:
std::vector <LPCSTR> waveFile;
std::fstream file( "data/sounds.txt", std::ios::in );
char line[20][255];
int a = 0;
while( !file.eof() )
{
file.getline( line[a], 255, ' ' );
file.getline( line[a], 255, '\n' );
waveFile.push_back(line[a]);
a++;
}
file.close();

Now all the elements of waveFile vector have proper values.

Does anyone know why my first code didn't work? I honestly do not know what even might be wrong... I would be evry grateful for any help!

Share this post


Link to post
Share on other sites
Advertisement

Hey,
I need to load paths to particular sounds from txt file and pass those paths to vector of LPCSTR type. Therefore I have some questions about the code I've made:
std::vector <LPCSTR> waveFile;
std::fstream file( "data/sounds.txt", std::ios::in );
char line[255];
while( !file.eof() )
{
file.getline( line, 255, ' ' );
file.getline( line, 255, '\n' );
waveFile.push_back(line);
}
file.close();

This code doesn't work... I have no certain idea why but out of while loop every waveFile element has the same path (the last one loaded from txt)...

I came up with a solution:
std::vector <LPCSTR> waveFile;
std::fstream file( "data/sounds.txt", std::ios::in );
char line[20][255];
int a = 0;
while( !file.eof() )
{
file.getline( line[a], 255, ' ' );
file.getline( line[a], 255, '\n' );
waveFile.push_back(line[a]);
a++;
}
file.close();

Now all the elements of waveFile vector have proper values.

Does anyone know why my first code didn't work? I honestly do not know what even might be wrong... I would be evry grateful for any help!


LPCSTR is a pointer to a character array, so in the first piece of code you are basically adding &line to the vector everytime (and thus every item will have the same value). The second piece of code works because you add different pointers (1 of 20) to the vector each time. You should use the string type instead of LPCSTR.

Edit: clarity

Share this post


Link to post
Share on other sites
Maybe you could have just reset line every loop. You could enter debug mode and loop through to see if (on your first example) line is being added to when getline uses it. if it is, just make sure you clear line before you call getline since maybe getline only adds to the end of your char line array

Share this post


Link to post
Share on other sites
Thanks a lot for help. Replacing LPCSTR with std::string helped.
I've been using LPCSTR cause I thought it is recommened type for XAUDIO2 operations...(this type was used in a tutorial). But it seems std::string works too

Share this post


Link to post
Share on other sites

I've been using LPCSTR cause I thought it is recommened type for XAUDIO2 operations...


std::string is the recommended type for strings in C++. If you are ever using an std::string and still need a C-style string (i.e. char*, for working with a C library, for example), you can use the std::string's c_str() method.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!