C++ 如何打印从不同功能的文本文件中读取的数据

C++ 如何打印从不同功能的文本文件中读取的数据,c++,C++,首先,我很抱歉我的英语不好 我想问的是,如何打印从不同功能的文本文件读取的数据 load()函数定义如下 void Picture::load(string filename) throw(string) { int x,y; string line; fstream infile(filename.c_str(), fstream::in); if (infile.is_open()) { if (!getline(infile, line)) throw stri

首先,我很抱歉我的英语不好

我想问的是,如何打印从不同功能的文本文件读取的数据

load()
函数定义如下

void Picture::load(string filename) throw(string)
{
int x,y;
string line;

fstream infile(filename.c_str(), fstream::in);

if (infile.is_open())
{
    if (!getline(infile, line))
        throw string("Unable to read the first line.");

    istringstream iss(line);

    if (!(iss >> height >> width))
        throw string("First line does not consist of two integers.");

    picture = new char*[width];

    for (x=0; x<width; x++)
        picture[x] = new char[height];

    for (y=0; y<height; y++)
    {
        getline(infile,line);

        if (line.length() < width)
            throw string("Line "+convertInt(y+1)+" in picture has an incorrect width.");
        else
            for (x=0; x<width; x++)
                set(x,y,line[x]);
    }

    infile.close();
    }

    else throw string("Unable to open file");
}

void Picture::print()
{
    // This function will print the data read on load function
}
void Picture::加载(字符串文件名)抛出(字符串)
{
int x,y;
弦线;
fstream infle(filename.c_str(),fstream::in);
if(infle.is_open())
{
如果(!getline(填充,行))
抛出字符串(“无法读取第一行”);
istringstream iss(线);
如果(!(iss>>高度>>宽度))
抛出字符串(“第一行不包含两个整数。”);
图片=新字符*[宽度];

对于(x=0;x将数据存储为类的私有成员:

private std::string data;
void loadAndPrint::load()
{
    in_file.open("infile.txt");
    in_file >> data;
}
void loadAndPrint::print()
{
    ofstream out_file("outfile.txt");
    if(!out_file.is_open()) throw myError;
    out_file << data;
}
私有std::字符串数据;
void loadAndPrint::load()
{
在_file.open(“infle.txt”);
在_文件>>数据中;
}
void loadAndPrint::print()
{
流输出文件(“outfile.txt”);
如果(!out_file.is_open())抛出myError;

out_file您可以通过引用第二个函数来传递fstream。这使您不必创建私有变量

编辑:

在看不到整个课堂的情况下,我不能确定,但您的讲师似乎希望您使用NTCA完成此作业。在不给出完整答案的情况下,请阅读本文,看看它是否回答了您的问题:


好吧,实际上这项作业,我的讲师已经定义了加载函数以及不能更改的类。阅读我最近编辑的问题。我包括了
load()
函数,我无法更改。因此,您唯一可以更改的是打印函数和类外的内容?是的。我需要完成定义的方法。我在打印加载的文本时遇到问题。嘿,谢谢您的评论,我的讲师给出了打印提示。实际上是关于
加载()
函数,
集合(x,y,行[x]);
是存储字符的集合。因此,在打印之前,我必须先完成
集合
函数。很抱歉浪费您的时间。我有一个私人成员
字符**图片
。您能参考我最近编辑的关于
加载()函数的问题吗?