简体   繁体   English

使用函数从文件输出

[英]Output from file using function

the title says it all标题说明了一切

This is my code: I have a class called House, where i define the values.这是我的代码:我有一个名为 House 的类,我在其中定义了值。

class HOUSE
{
public:
    int id;
    string 1;
    string 2;
    string 3;
    int an;


};

template<class Type>
class table
{
public:

    vector<Type> V;
    //double inceput;
    //double sfirsit;
    //int comparatii;
    //int interschimbari;   
public:
    table();
    void print();
    void liniar();
};

template<class Type>
table<Type>::table()
{
    ifstream file("file.txt");
    ifstream file1("file1.txt");

    if (file.fail() || file1.fail())
    {
        cerr << "Eroare la deschiderea fisierului!" << endl;
        _getch();
        exit(1);
    }

    HOUSE* value = new HOUSE;

while (!file.eof() || file1.fail())
{
    file >> value->id;
    file >> value->tara;
    file >> value->brand;
    file >> value->culoare;
    file >> value->an;

    this->V.push_back(*value);
}


file.close();

} }

The print function for the values值的打印功能

template<class Type>
void table<Type>::print()
{

    cout << endl << setw(50) << "AFISAREA DATELOR" << endl;
    cout << setw(5) << "Id" << setw(15) << "1" << setw(20) << "2" << setw(17) << "3" << setw(20) << "an" << endl << endl;
    for (int i = 0; i < this->V.size(); i++)
    {
        cout << setw(5) << this->V.at(i).id << setw(15)
            << this->V.at(i).1<< setw(17)
            << this->V.at(i).2<< setw(17)
            << this->V.at(i).3<< setw(25)
            << this->V.at(i).an << endl;
    }
    cout << endl << "Dimensiunea tabelului  n= " << V.size() << endl;

}


{
        file >> value->id;
        file >> value->1;
        file >> value->2;
        file >> value->3;
        file >> value->an;

        this->V.push_back(*value);
    }


    file.close();

}

in main在主要

int main() {

    table<MOBILE>* file = new table<MOBILE>();
    table<MOBILE>* file1 = new table<MOBILE>();

 file ->print();    
 file1 ->print();

This is full code as requested.这是要求的完整代码。 Need make it somehow print the data from file1 and file2 .需要让它以某种方式打印 file1 和 file2 中的数据。 thx谢谢

The problem is idk if called right.如果调用正确,问题是 idk。 Because file ->print();因为文件 ->print();
file1 ->print();文件 1 -> 打印(); both print data from the file only There are no errors at all两者都只从文件中打印数据根本没有错误

There's lots and lots wrong with your code.你的代码有很多错误。 but I'm going to ignore that and just answer what I think is your actual question.但我会忽略这一点,只回答我认为是你的实际问题。

You want two table objects, one of which reads from file.txt and the other reads from file1.txt .您需要两个table对象,其中一个从file.txt读取,另一个从file1.txt读取。 To do that you should pass the file name into the table::table constructor, so it knows which file to read from.为此,您应该将文件名传递给table::table构造函数,以便它知道要读取哪个文件。 Like this像这样

template<class Type>
class table
{
    ...
public:
    table(const char* filename); // constructor takes filename parameter
    ...
};


template<class Type>
table<Type>::table(const char* filename)
{
    ifstream file(filename); // open filename
    if (file.fail())
...


int main() {

    table<MOBILE>* file = new table<MOBILE>("file.txt"); // read from file.txt
    table<MOBILE>* file1 = new table<MOBILE>("file1.txt"); // read from file1.txt

    file ->print();    
    file1 ->print();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM