2

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>'

このエラーの原因はわかりませんが、関数を作成する前にこのエラーが発生しなかったため、作成した関数と関係があると思われます。

どんな助けでも大歓迎です、どんな返事でも前もって感謝します。

4

2 に答える 2

3

ストリームを値で渡すように定義readfileしました。これには、ストリームのコピー コンストラクターへのアクセスが必要です。ストリームをコピーすることは想定されていないため、これは非公開です。代わりに参照渡しでストリームを渡します (壊れreadfileているため、ループを修正するために書き直します)。while (!xxx.eof())

于 2013-05-07T04:29:54.563 に答える
2

参考にする

string readfile(ifstream & ifs) {
//                       ^

fstreamコピーできないからです。

于 2013-05-07T04:29:16.087 に答える