我習慣於更高級的語言(java,python等),這是顯而易見的。我正在嘗試將用戶輸入的字符串傳遞給cin,這是要打開的文件的名稱。似乎有某種指針瘋狂錯誤,我的代碼不會編譯。 我刪除了一些我的代碼,使其更清晰。將一個字符串傳遞給file.open();
#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;
}
這是編譯時錯誤。
<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>
(我知道有對MD5哈希庫,但我想了解哈希是如何工作的,並最終哈希衝突)
在發佈之前,請將您的行號和額外間距排除。這使得很難複製和粘貼代碼進行測試。 –
@LokiAstari,我發現爲時已晚,但在大多數編輯器中,您可以按Ctrl + Alt + Selection選擇包含數字的列,並一次刪除它們。自從我發現它以來,爲我節省了很多時間。 – chris