Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm getting error C2664 on some I/O code

void BinaryTree::InitializeFromFile(string Filename){
ifstream inFile;
inFile.open(Filename, fstream::binary);
if(inFile.fail()){
    cout<<"Error in opening file "<<Filename;
    return;
}
 for(int i=0;i<=255;i++) Freq[i]=0;
  char c;
  inFile.get(c);
  while(!inFile.eof()){
    Freq[c] ++;
    inFile.get(c);
  }
}  



HuffmanTree.cpp(293) : error C2664: 'void std::basic_ifstream<_Elem,_Traits>::
open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 
from 'std::string' to 'const wchar_t *'
1>    with
1>    [
1>        _Elem=char,
1>        _Traits=std::char_traits<char>
1>    ]
1>    No user-defined-conversion operator available that can perform this 
      conversion, or the operator cannot be called

Line 293 is inFile.open(Filename, fstream::binary);

like image 567
Azreal Avatar asked Sep 20 '26 22:09

Azreal


2 Answers

Use Filename.c_str() instead - open() doesn't take a std::string as a parameter for the filename.

like image 128
Timo Geusch Avatar answered Sep 22 '26 10:09

Timo Geusch


use Filename.c_str() instead of Filename in the call of ifstream::open

like image 35
f4. Avatar answered Sep 22 '26 11:09

f4.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!