【问题标题】:How to get void print functions (that are within classes) printed to a text file如何将无效打印函数(在类中)打印到文本文件中
【发布时间】:2020-04-16 21:49:56
【问题描述】:
#include <iostream>
#include  <fstream>
#include <string>

using namespace std;

class Person                     //class Person
{
    public:                             //declare variables in the Person class
        string lastName, firstName, address, city, state, zip, phone;
        Person();

        void printPerson()                              //function used to print out the content inside this class
        {
            cout << firstName << " ";
            cout << lastName << endl;
            cout << address << ", ";
            cout << city << ", ";
            cout << state << " ";
            cout << zip << endl;
            cout << "Phone Number: " << phone << endl;
        }

};

class Customer : public Person {                    //declare class Customer; Customer is a derived class of Person
    public:
        int customerNumber;                       //declare variables 
        bool mailingList;
        string comments;
        Customer();

        void printCustomer()                     //function used to print the content in the Customer class as well as the Person class
        {

            printPerson();         

            cout << "Customer Number: " << customerNumber << endl;

            if (mailingList == true)
            {
                cout << "Mailing List: True" << endl;
            }
            if (mailingList == false)
            {
                cout << "Mailing List: False" << endl;
            }

            cout << "Comments: " << comments << endl;
        }
};

int main()
{
    ofstream outfile;
    outfile.open("testOutput.txt", ios::out);

    PreferredCustomer Igottaquestion;                                 //create an instance for PreferredCustomer
    Igottaquestion.lastName = "Igottaquestion";
    Igottaquestion.firstName = "Ed";
    Igottaquestion.address = "4901 Evergreen";
    Igottaquestion.city = "Dearborn";
    Igottaquestion.state = "MI";
    Igottaquestion.zip = "48126";
    Igottaquestion.phone = "313-436-9145";
    Igottaquestion.customerNumber = 1;    
    Igottaquestion.mailingList = true;
    Igottaquestion.comments = "class quentioner";
    Igottaquestion.printPreferredCustomer();


    Customer Kool;                                   //create an instance for Customer
    Kool.lastName = "Kool";
    Kool.firstName = "James";
    Kool.address = "1313 Colin Blvd";
    Kool.city = "Dearborn Heights";
    Kool.state = "MI";
    Kool.zip = "48127";
    Kool.phone = "313-836-9168";
    Kool.customerNumber = 3;
    Kool.mailingList = false;
    Kool.comments = "Class Answerer";
    Kool.printCustomer();

    cout << endl;


    system("pause");
    return 0;
}

我的问题是,不仅在屏幕上,我还需要将结果打印/输出到文本文件。

通常,我可以这样做:

流输出文件;

outfile.open("testOutput.txt", ios::out);

输出文件

但是,在这种情况下,如何将无效打印函数(嵌入在类中)打印到文本文件中?

【问题讨论】:

  • 你能编辑函数吗?除非您被允许更改功能,否则我认为您无能为力。
  • 根据 hw 指令,是的,我可以编辑函数。我也尝试编辑这些函数,但我认为在每个 void print 函数中打开输出文件只会一遍又一遍地重写文件,最终,只有最后一个被调用的函数会被打印到文件中。
  • 如果程序打印出正确的输出,为什么不使用一个简单的实用程序 tee(在命令行上,无需修改程序)或重定向输出?
  • 提示:C++ 使用std::ostream&amp; 将数据格式化为文本并将其发送到……某处。 std::cout 就是一个例子;它将文本发送到“标准输出”,它可以连接到终端或屏幕。 std::ofstream 类型的对象是另一个例子;它将文本发送到您告诉它打开的文件。

标签: c++ class void


【解决方案1】:

这很简单,只需将您想要输出的位置作为参数传递给您的函数即可。像这样

   void printPerson(std::ostream& out) // out is the destination we are writing to
    {
        out << firstName << " ";
        out << lastName << endl;
        out << address << ", ";
        out << city << ", ";
        out << state << " ";
        out << zip << endl;
        out << "Phone Number: " << phone << endl;
    }

printCustomer 等的相同更改。

然后你使用这样的函数

  ofstream outfile;
  outfile.open("testOutput.txt", ios::out);

  Customer Kool;
  ...
  Kool.printCustomer(outFile); // write the customer to outfile
  outFile << endl;

  Person Aplus;
  ...
  Aplus.printPerson(outFile); // write the person to outfile

【讨论】:

  • 我要指出这一点的一部分是,如果你这样做Kool.printCustomer(std::cout);,你仍然有原来的行为。
  • 是的,std::ostream&amp; 与任何输出流兼容,包括文件流和std::cout
  • 感谢您的帮助!我以前从未尝试过这样做,所以这可能是一个愚蠢的问题,但我应该在嵌入式打印函数的 () 中放入什么。我刚刚在我的问题中编辑了我的代码,如果您再次复制并粘贴我的代码,第 47 行和第 105 行是我遇到问题的地方。我试图把“out”放在里面,但是当我运行程序时得到一个空的文本文件
  • 如果我将“std::ostream& out”放在这些括号内,我会得到红色下划线......而“out”是我能想到的唯一可以放在那里并获胜的东西t 给我红色下划线,然后我会得到一个空的 txt 文件
  • @EmmaG 再看我写的,你没有正确复制。
【解决方案2】:

你应该为你的班级重载 https://docs.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes?view=vs-2019

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2017-08-07
    • 2015-05-05
    • 1970-01-01
    相关资源
    最近更新 更多