Hello. Could someone please help me understand what's going on with this code?
What I'm trying to do is read each line of a file into a vector<char const *>. I feel like the answer is probably extremely obvious but I just can't figure it out.
int main( int argc, char ** argv )
{
int i = 0;
std::vector<char const *> vec;
std::ifstream file("abcd.txt");
if ( file.is_open() ) {
std::string temp;
while ( std::getline(file, temp).good() ) {
std::cout << "temp.c_str() " << i << ": " << temp.c_str() << "\n\n";
vec.push_back(temp.c_str());
for (int j = 0; j < vec.size(); ++j) {
std::cout << " vec.at(" << j << ") : " << vec.at(j) << "\n";
}
std::cout << "\n";
++i;
}
}
}
This is the text file.
aaaaaa
bbbbbbbbbbbb
ccccccc
ddddddddd
This is the output:
temp.c_str() 0: aaaaaa
vec.at(0) : aaaaaa
temp.c_str() 1: bbbbbbbbbbbb
vec.at(0) : XT
vec.at(1) : bbbbbbbbbbbbtemp.c_str() 2: ccccccc
vec.at(0) : XT
vec.at(1) : ccccccc
vec.at(2) : ccccccctemp.c_str() 3: ddddddddd
vec.at(0) : XT
vec.at(1) : ddddddddd
vec.at(2) : ddddddddd
vec.at(3) : ddddddddd