简体   繁体   中英

Reading From Text File In c++

I am writing a program that takes a single input line consisting of a pattern and the name of a text file. The program will then search the given text file for the given pattern.

I take the input and convert it into two strings: pattern and file_name I get an error on the following line:

ifstream text(file_name);

When I change it to the following, it compiles fine.

ifstream text("file_name");

Obviously, this is not what I want though since the file name must be determined by the user's input.

How can I make this compile given a changing file name?

For C++03, ifstream takes const char* parameter, you could try:

ifstream text(file_name.c_str());

While with C++11, your first statement should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM