C++ 将字符串传递给 file.open();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10966446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:40:59  来源:igfitidea点击:

Passing a string to file.open();

c++stringpointers

提问by Muricula

I am used to higher level languages (java, python etc.), where this is dead obvious. I am trying to pass a string the user inputs to cin, the name of a file to open. There appears to be some sort of pointer madness error, and my code will not compile. I deleted some of my code to make it more clear.

我习惯了高级语言(java、python 等),这很明显。我试图将用户输入的字符串传递给要打开的文件名 cin。似乎存在某种指针疯狂错误,我的代码无法编译。我删除了一些代码以使其更清晰。

   #include <iostream>
   #include <fstream>
   using namespace std;

   string hash(string filename);

   int main(){
           cout << "Please input a file name to hash\n";
           string filename;
           cin >> filename;
           cout <<hash(filename);
           return 0;
   }


    string hash(string filename){
            file.open(filename);
            if(file.is_open()){

                   file.close();
            }

            return returnval;
    } 

Here is the compile time error.

这是编译时错误。

<code>
$ g++ md5.cpp
md5.cpp: In function ‘std::string hash(std::string)':
md5.cpp:22: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)'
/usr/include/c++/4.2.1/fstream:518: note: candidates are: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
</code>

(I know that there are libraries for md5 hashes, but I am trying to learn about how the hash works, and eventually hash collision)

(我知道有用于 md5 散列的库,但我正在尝试了解散列的工作原理,并最终了解散列冲突)

回答by chris

open()takes a C-style string. Use std::string::c_str()to get this:

open()采用 C 风格的字符串。使用std::string::c_str()得到这个:

file.open (filename.c_str());

In order to use just a string, as pointed out below, you'll need to use a compiler with C++11 support, as the overload was added for C++11.

为了仅使用字符串,如下所述,您需要使用支持 C++11 的编译器,因为 C++11 添加了重载。

The reason it's not like Java etc. is that it came from C. Classes didn't exist in C (well, not nearly as well as they do in C++), let alone a Stringclass. In order for C++ to provide a string class and keep compatibility, they need to be different things, and the class provides a conversion constructor for const char * -> std::string, as well as c_str()to go the other way.

它不像 Java 等的原因是它来自 C。C 中不存在类(嗯,不像在 C++ 中那样好),更不用说String类了。为了让 C++ 提供一个字符串类并保持兼容性,它们需要是不同的东西,并且该类为 提供了一个转换构造函数const char * -> std::string,以及c_str()走另一条路。

Consider passing the argument (and maybe the return too) as const std::string &as well; no unnecessary copies. The optimization would probably catch those, but it's always good to do.

也考虑传递参数(也可能是返回值)const std::string &;没有不必要的副本。优化可能会捕捉到这些,但这样做总是好的。