简体   繁体   English

从文本文件中读取在c ++中

[英]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: 我接受输入并将其转换为两个字符串:pattern和file_name我在以下行中收到错误:

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: 对于C ++ 03, ifstream采用const char*参数,您可以尝试:

ifstream text(file_name.c_str());

While with C++11, your first statement should work. 使用C ++ 11时,您的第一个语句应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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