【问题标题】:How to make output function to write formatted output to both screen and an output file如何使输出功能将格式化的输出写入屏幕和输出文件
【发布时间】:2017-12-01 05:42:44
【问题描述】:
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

void getInformationKeyBoard(int, string[], int[]);
bool openFile(ifstream &infile, string fileName);
void display(int size, string array[]);
void read2Array(ifstream &infile, int size, string array[]);
void printReport(string name[], int score[], int NumberOfStudent);
int main()
{
    const int size = 1024;
    string Name[size], scoreFile[size];
    int score[size];
    int NumberOfStudent;
    const int SIZE = 7;

    ifstream inFile;

    char choice;
    cout << "You want to enter your scores by your keyboard (A) or from your input (B): ";
    cin >> choice;
    if (choice == 'a' || choice == 'A') // It will take information from keyboard
    {
        cout << "How many students do you want to enter: ";
        cin >> NumberOfStudent;
        getInformationKeyBoard(NumberOfStudent, Name, score);
        printReport(Name, score, NumberOfStudent);
    }

    else if (choice == 'b' || choice == 'B') // It will take information from file
    {

        string name;
        char again = 'Y';
        bool close = false;

        cout << "Enter name of file: ";
        cin >> name;
        openFile(inFile, name);
        read2Array(inFile, SIZE, scoreFile);
        display(SIZE, scoreFile);




    }
    else // If you choice is not A,a or B,b
        cout << "Your did not follow the right instruction.";


    cout << endl << endl;
    system("pause");
    return 0;

}

// Open file
bool openFile(ifstream &infile, string fileName){
    infile.open(fileName);
    if (infile)
        return true;
    return false;
}

void getInformationKeyBoard(int size, string Names[], int scores[]) // Information from keyboard
{
    for (int i = 0; i < size; i++)
    {
        cout << i + 1 << ". Student First Name and Last Name: ";
        cin.ignore();
        getline(cin, Names[i]);
        do
        {
            cout << i + 1 << ". Enter the score between 1 and 100: ";
            cin >> scores[i];
        } while (scores[i] > 100 || scores[i] < 0);

    }
}



void read2Array(ifstream &infile, int size, string array[]){
    int index = 0;
    string line;

    while (getline(infile, line)){
        array[index] = line;
        ++index;
    }
}

// Display array
void display(int size, string array[]){
    for (int index = 0; index < size; ++index){
        cout << array[index] << endl;
    }
}


void printReport(string name[], int score[], int NumberOfStudent)
{
    int lowest, highest, mean;
    cout << "Enter lowest score: ";
    cin >> lowest;
    cout << "Enter highest score: ";
    cin >> highest;
    cout << "Enter mean score: ";
    cin >> mean;

    cout << "================================================================================";
    cout << setw(10) << "Number of scores = " << NumberOfStudent << endl;
    cout << setw(10) << "Lowest Score = " << lowest << endl;
    cout << setw(10) << "Highest Score = " << highest << endl;
    cout << setw(10) << "Mean Score = " << mean << endl;
    cout << "Name" << setw(15) << "Score" << setw(15) << "IsLowest" << setw(15) << "IsHighest" << setw(15) << "Mean" << endl;
    cout << "-----------------------------------------------------------------" << endl;
    cout << name[NumberOfStudent] << endl;
    cout << endl;

}

包含分数、最低、最高和平均分数的标题统计部分,然后是每个学生的详细信息 - 每行一个。

我坚持将输出输出到屏幕。我希望我的输出看起来像这样

Number of scores = 3
Lowest Score  = 82
Highest Score = 92
Mean Score = 87
Name    Score    IsLowest    IsHighest    >=Mean
F1 L1    82         Y              N          N
F2 L2    87         N              N          Y
F3 L3    92         N              Y          Y

【问题讨论】:

    标签: c++ arrays function if-statement iomanip


    【解决方案1】:

    也许您可以传入一个 ostream 对象作为参数,然后将函数中所有出现的 cout 替换为该参数的名称。然后你可以运行该函数两次

    printReport(cout, other args);
    printReport(outFile, other args);
    

    (或者将函数设置为调用自身,这样您就不必每次都输入 cout)。 这种方法的唯一问题是我看到您在函数内部获取输入。如果要使用上述方法,则必须移动一些东西。相反,我会设置一个不同的函数,该函数将字符串作为输入并将所述字符串插入到 cout 和您的文件中。所以不用写了

    cout<<"hi";
    file<<"hi";
    

    你可以的

    ...
    functionName("hi", outfile);
    ...
    void functionName(string str, ostream outfile)
    {
    cout<<str;
    outfile<<str;
    }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      一般来说,以下函数可用于在文件中复制控制台输出。

      #include <stdarg.h>     /* va_list, va_start, va_arg, va_end */
      
      void print(FILE *f, char const *fmt, ...) {
          va_list ap;
          //Normal console print
          va_start(ap, fmt);
          vprintf(fmt, ap);
          va_end(ap);
          //Printing to file
          if (f != NULL) {
              va_start(ap, fmt);
              vfprintf(f, fmt, ap);
              va_end(ap);
          }
      }
      

      如果您只想要控制台输出,只需拥有

      print(NULL, "%d\t%d\n", 1, 100);//include any needed C style formatting here

      如果您希望将控制台输出保存到文件中,只需将适当的文件指针作为第一个参数传递。

      FILE *fp = fopen("logfile.txt","a");
      print(fp, "%d\t%d\n, 1, 100);//logs the console output to logfile.txt
      

      控制台窗口格式也将保留在文本文件中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 2011-04-18
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 1970-01-01
        相关资源
        最近更新 更多