# Loading data from txt file to LPCSTR variable

This topic is 2554 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites

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 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 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 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 on other sites
Not DirectX related; Moving to General Programming.

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013654
×