Reputation: 13
I am trying to make a program that will read from one file and output to another file a different value than read in, based on a input integer. I am getting errors on opening files. No matching call function
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <stdio.h>
using namespace std;
int main()
{
char option, m, c;
int amount, n, k;
string fname, dname, ename;
ofstream nstream;
ifstream istream;
cout << "Do you want to encrypt of decrypt? Enter D or E.";
cin >> option;
cout << endl;
while (option != 'D' && option != 'E')
{
cout << "That is not acceptable" << endl;
cout << "Do you want to encrypt of decrypt? Enter D or E.";
cin >> option;
cout << endl;
}
if (option == 'E')
{
cout << "Enter the name of the file that you want to encrypt: ";
cin >> fname;
istream.open(fname);
cout<<endl;
cout << "Enter the name of the file that you want to output: ";
cin >> ename;
nstream.open(ename);
cout<<endl;
}
if (option == 'D')
{
cout << "Enter the name of the file that you want to decrypt: ";
cin >> dname;
istream.open(dname);
cout<<endl;
cout << "Enter the name of the file that you want to output: ";
cin >> ename;
nstream.open(ename);
cout<<endl;
}
cout << "Enter the number of digits that you want to de/encrypt(integers): ";
cin >> k;
istream >> m;
while (m >= 0);
{
if (m > 65 && m < 90);
{
c = m + k;
while (c > 90)
{
c = c - 26;
}
}
if (m > 97 && m < 122);
{(c = m + k);
while (c > 122)
{
c = c - 26;
}
}
if(m >= 0)
{
c = m + k;
}
}
nstream << c;
return 0;
}
error messages
In function ‘int main()’:
homework4.cpp:39:21: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
istream.open(fname);
^
homework4.cpp:39:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:541:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s, ios_base::openmode __mode = ios_base::in)
^
/usr/include/c++/4.9/fstream:541:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:43:21: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&)’
nstream.open(ename);
^
homework4.cpp:43:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:716:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s,
^
/usr/include/c++/4.9/fstream:716:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:51:21: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::string&)’
istream.open(dname);
^
homework4.cpp:51:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:541:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s, ios_base::openmode __mode = ios_base::in)
^
/usr/include/c++/4.9/fstream:541:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
homework4.cpp:55:21: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::string&)’
nstream.open(ename);
^
homework4.cpp:55:21: note: candidate is:
In file included from homework4.cpp:4:0:
/usr/include/c++/4.9/fstream:716:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s,
^ /usr/include/c++/4.9/fstream:716:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘const char*’
Upvotes: 1
Views: 2647
Reputation: 2781
In your code, fname
, dname
, and ename
are variables of type std::string
, by default. However, the fstream::open
requires a const char *
value passed to it for a filename.
Here is how the function should be used:
void open (const char* filename, ios_base::openmode mode = ios_base::in | ios_base::out);
Obtained from C++ Reference: std::fstream::open
To do this, add the .c_str()
function to the end of the filename variable:
istream.open(fname.c_str());
nstream.open(ename.c_str());
istream.open(dname.c_str());
This will remove that error.
Upvotes: 1