Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading data from txt file to LPCSTR variable

Started by savail Dec 20, 2011 at 3:57 PM 4 replies 1.2k views
Original Post
savail
savail
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!
LegendInStone
LegendInStone

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
iedoc
iedoc
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
savail
savail
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
Cornstalks
Cornstalks

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.
Evil Steve
Evil Steve
Not DirectX related; Moving to General Programming.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.