gpt4 book ai didi

c++ - 重载 cin 以获取 ifstream 而不是 char* fname?

转载 作者:行者123 更新时间:2023-11-28 07:52:57 24 4
gpt4 key购买 nike

<分区>

我正在尝试创建一个文件操纵器,所以当我 cin >> filename 时,它将读取文件并计算值...当然我要添加更多内容,但我想继续获取文件内容的基础。

我想要实现的是通过 cout 提示用户输入文本文件名,然后当他输入时,我将在下一行代码中执行另一个 cin >> readfile 然后编译器将运行运算符重载并计算出内容。

我如何实现它。下面是我的代码。

问题不是接收 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;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com