简体   繁体   English

C++ ostream 参数

[英]C++ ostream parameters

I am building a project that solves a boggle board.我正在构建一个解决boggle board的项目。 The two functions I'm interested in at the moment are我目前感兴趣的两个功能是

void Boggle::SolveBoardHelper(bool printBoard, int row, int column, int rowMax, int columnMax,
                              ofstream& output, int numSteps, int steps[4][4], string currPath)


void Boggle::SolveBoard(bool printBoard, ostream &output) {
    ofstream outputFile;
    outputFile(output); // stuck here
    string currPath; // used to track the current path on the board

    for(int pos_x = 0; pos_x < BOARD_SIZE; pos_x++){
        for(int pos_y = 0; pos_y < BOARD_SIZE; pos_y++)
            SolveBoardHelper(print_or_not, pos_x, pos_y, BOARD_SIZE, BOARD_SIZE, outputFile, numSteps, visited, currPath);
    }  

}

My area of confusion is that when the user calls SolveBoard to solve a board, they have the option of passing "cout" or an output file into the ostream parameter.我的困惑之处在于,当用户调用 SolveBoard 来求解棋盘时,他们可以选择将“cout”或输出文件传递到 ostream 参数中。 If the user decides to call the function and pass in an output file, say "output.txt", how do I account for this?如果用户决定调用该函数并传入一个输出文件,比如“output.txt”,我该如何解释? How do I take the output file passed into the SolveBoard parameter by the user, and then plug that into the SolveBoardHelper function so that I can write to that file after the board has been solved?我如何获取用户传递给 SolveBoard 参数的输出文件,然后将其插入到 SolveBoardHelper 函数中,以便在解决板后写入该文件?

My previous attempt included me just passing a predetermined output file into the parameter, but I don't know what file the user will choose to output to:我之前的尝试包括我只是将一个预定的输出文件传递到参数中,但我不知道用户会选择输出到哪个文件:

void Boggle::SolveBoard(bool printBoard, ostream &output) {
   
    string outputFile = "solve_output_test.txt"; // write to the already created solve_output_test.txt file
    ofstream outFile(outputFile); // assign it to ofstream variable outFile
    if(!outFile){
        cout << "Error opening file" << endl; // file open check
    }
    string currPath; 
    
    for(int pos_x = 0; pos_x < BOARD_SIZE; pos_x++){
        for(int pos_y = 0; pos_y < BOARD_SIZE; pos_y++) 
            SolveBoardHelper(print_or_not, pos_x, pos_y, BOARD_SIZE, BOARD_SIZE, outFile, numSteps, visited, currPath);
    }   
}
void Boggle::SolveBoard(bool printBoard, ostream &output) {
    // remove the two lines below and just use `output` in the rest of the function:
    //ofstream outputFile; 
    //outputFile(output);
    
    for(int pos_x = 0; pos_x < BOARD_SIZE; pos_x++) {
        for(int pos_y = 0; pos_y < BOARD_SIZE; pos_y++)
            SolveBoardHelper(print_or_not, pos_x, pos_y, BOARD_SIZE, BOARD_SIZE, output,
                             numSteps, visited, currPath);
    }  
}

Then call the function with然后调用函数

SolveBoard(the_bool, std::cout);

or或者

std::ofstream file("output.txt");
SolveBoard(the_bool, file);

You also need to redefine SolveBoardHelper to take an ostream instead:您还需要重新定义SolveBoardHelper以采用ostream

void Boggle::SolveBoardHelper(bool printBoard, int row, int column, int rowMax,
                              int columnMax, ostream& output, int numSteps,
                              int steps[4][4], string currPath)

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

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