我运行此代码但我的编译器不想运行它并在第47行给我错误 infile.open(fileloc,IOS ::在);
有什么建议吗?谢谢
#include<iostream>
#include<iomanip>
#include<sstream>
#include<fstream>
using namespace std;
void encrypt(); // encrypt function protype
void decrypt();
int main()
{
int choice;
bool done = false;
while(!done){
cout << "What you want to do?\n1) Encrypt a file.\n2) Decrypt a file." << endl;
cin >> choice;
if (choice == '1')
{
encrypt();
done = true;
}
if (choice == '2')
{ decrypt();
done = true;
}
else
{ cout <<"Invalid input try again"<<endl;
}
}
}
void encrypt()
{
string outfile;
string fileloc;
string x;
char y;
fstream infile;
fstream outputFile;
cout << "What is the name of the file you want to encrypt? " << endl;
cin >> fileloc;
infile.open(fileloc,ios::in);
while(!infile){
cout << "Your file does not exist, enter a valid file name..."<< endl;
cin >> fileloc;
infile.open(fileloc,ios::in);
}
cout << "What is the name of your output file?" << endl;
cin >> outfile;
outputFile.open(outfile,ios::out);
while(infile>>noskipws>>y){
outputFile << char(y+10);
}
}
void decrypt()
{
string outfile;
string fileloc;
char y;
fstream infile;
fstream outputFile;
cout << "What is the name of the file you want to decrypt? " << endl;
cin >> fileloc;
infile.open(fileloc,ios::in);
while(!infile){
cout << "Your file does not exist, enter a valid file name..."<< endl;
cin >> fileloc;
infile.open(fileloc,ios::in);
}
cout << "What is the name of your output file?" << endl;
cin >> outfile;
outputFile.open(outfile,ios::out);
while(infile>>noskipws>>y){
outputFile << char(y-10);
}
}
该程序应通过向文件中的每个ASCII字符添加10来进行基本加密。
答案 0 :(得分:0)
infile.open(fileloc, ios::in)
这并不适用于所有标准。如果您正在使用C ++ 11,那很好。但是,许多编译器默认使用较旧的标准。您可以尝试使用
infile.open(fileloc.c_str(), ios::in)
这将获取std::string
的c-string表示形式,该代码与fstream::open
兼容。
如果看一下here,您会发现C ++过去只允许const char *
作为路径的参数。
您还可以确保您当然使用兼容的标准,支持const std::string&
作为路径的参数类型。