私はファイルマニピュレーターを作成しようとしているので、cin >> filename
ファイルを読み取って値を計算するとき...もちろん、さらにスタッフを追加しますが、ファイルのコンテンツを取得する基本を進めたいと思います。
私が達成したいのは、ユーザーにテキストファイル名の入力を促す cout を実行することです。ユーザーが入力するとcin >> readfile
、コードの次の行で別の操作を行い、コンパイラが演算子のオーバーロードを実行してコンテンツを cout します。
どうすれば達成できますか。以下は私のコードです。
質問は を取り込む代わりにchar* fname
、代わりに ifstream を送信できますか?
だから私は何かをすることができます
ifstream infile(filename.c_str());
cin >> infile
以下は私の現在のコードです
#include <iostream>
#include <fstream>
#include <string>
#include "point2d.cpp"
#include "line2d.cpp"
#include <iomanip>
using namespace std;
struct myself
{
string myname;
};
istream &operator>> (istream &stream, myself &myself )
{
cout << "Enter my name";
stream >> myself.myname;
return stream;
}
istream &operator>> (istream &in, char* fname)
{
ifstream inFile;
inFile.open(fname);
char ch1;
while (inFile.get(ch1))
{
cout << ch1;
}
}
int main()
{
string filename;
cout << "Enter file name: ";
cin >> filename;
// was thinking of
// ifstream infile(filename.c_str());
// then read infile with cin
//how do i cin filename and read its content.
return 0;
}