Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:45:18+00:00 2026-06-05T14:45:18+00:00

I am used to higher level languages (java, python etc.), where this is dead

  • 0

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.

#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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:45:20+00:00Added an answer on June 5, 2026 at 2:45 pm

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

    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.

    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 String class. 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.

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used to generate random string in the following way (now I've switched to
By lower level package I mean a package a.b.c.d and by higher level package
From my understanding Audio Queue Services are a higher level API than Audio Units,
So I have this huge tree that is basically a big switch/case with string
Desired: I'd like to be have my git commits reference some higher order/level task
Used to be quite happy with the Ctrl + Shift + F10 shortcut in
Used code I found on SO to use the COM based Acrobat Reader to
I used a tool a few months ago that scanned a specified website and
I used mmap(just try to understand how mmap works) to allocate 96k anonymous memory,
There used to be an option in the submit wizard where you can specify

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.