2014-04-16 51 views
0

首先,我为我的糟糕英语感到抱歉。如何打印从不同功能的文本文件中读取的数据

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

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 
} 

如何将这两个函数关联起来以便加载和打印?

对不起,如果这个问题之前已经问过。

回答

0

数据存储为你的类的私有成员:

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; 
} 
+0

我有一个私有成员'字符** picture'。你可以参考我最近编辑的关于'load()'函数的问题吗? – user3538277

0

您可以通过引用传递的fstream到第二功能。这使您不必制作私有变量。

编辑:

没有能够看到整个班级,我不能肯定,但它看起来像你的老师希望你使用NTCAs此作业。如果不只是给你一个完整的答案,看看这篇文章,看看它是否回答你的问题:

http://www.cplusplus.com/doc/tutorial/ntcs/

+0

嗯,实际上这个任务,我的讲师已经定义了加载函数和不能改变的类。阅读我最近编辑的问题。我包含了我无法改变的'load()'函数。 – user3538277

+0

因此,唯一可以改变的是打印功能和课外作业? – GGMG

+0

是的。我需要做的是完成定义的方法。我在打印加载的文本时遇到问题。 – user3538277

相关问题