2 つのファイルの内容を取得し、それらを 3 つ目のファイルに追加する非常に基本的なプログラムを作成しています。私のコードは次のとおりです。
#include "std_lib_facilities.h"
string filea, fileb,
contenta, contentb;
string getfile()
{
string filename;
bool checkname = true;
while (checkname)
{
cout << "Please enter the name of a file you wish to concatenate: ";
cin >> filename;
ifstream firstfile(filename.c_str());
if (firstfile.is_open())
{
cout << filename << " opened successfully.\n";
checkname = false;
}
else cout << "Cannot open " << filename << endl;
}
return filename;
}
string readfile(ifstream ifs)
{
string content;
while (!ifs.eof())
{
string x;
getline(ifs, x);
content.append(x);
}
return content;
}
int main()
{
ifstream firstfile(getfile().c_str());
ifstream secondfile(getfile().c_str());
ofstream newfile("Concatenated.txt");
newfile << readfile(firstfile) << endl << readfile(secondfile);
system("PAUSE");
firstfile.close();
secondfile.close();
newfile.close();
return 0;
}
ただし、このコードをコンパイルしようとすると、次のエラーが発生します。
error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
このエラーの原因はわかりませんが、関数を作成する前にこのエラーが発生しなかったため、作成した関数と関係があると思われます。
どんな助けでも大歓迎です、どんな返事でも前もって感謝します。