Open txt file with c_str()

Hello, everyone! It's my first post on this forum~! And I'm new to C++.
I did look for solutions from previous posts to my question, but I failed to find one.

My programme reads student_numbers (string, e.g. 123456) from one txt file.
And I want to open txt files with the name <student_number>.

People suggested infile.open(student_number.c_str());

However, it does not work in my case. If I use the above method, am I missing ".txt"? And how should I do it?

Thanks!




1
2
3
4
std::string student_number = "123456" ;
const std::string file_name = student_number + ".txt" ;
std::ifstream infile( file_name.c_str() ) ; // C++98 or later
// or: std::ifstream infile( file_name ) ; // C++11 or later 
Oh it's about const string!! It works now!!
Thanks!!
Topic archived. No new replies allowed.