简体   繁体   English

快速Q-无法写入文件功能输出

[英]Quick Q - Can't Write to File the Output of a Function

Code : 代码

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

void printVector (const vector<string>& theVect, vector<bool>& bArray, int nItems){
    for (int i = 0; i < nItems; ++i)
       if (bArray[i] == true)
         cout << theVect[i] << " ";
         outFile << theVect[i];
    cout << "\n";
    outFile << "\n";
}

void nCombination(const vector<string> &Vect,int n, int r){

    vector<bool> myBits(n, false);  // everything is false now
    int count = 1; 
    for (size_t i = n-1; i >= 0 && count <= r; --i, ++count){
        myBits[i] = true;
    }
    do  // start combination generator
    {
       printVector(Vect, myBits, n );
    } while (next_permutation(myBits.begin(), myBits.end()));;  // change the bit pattern
}

void nPermutation(vector<string> o, int r){
    do {
        for(int count = 0; count < r; count++){
        cout << o[count] << " " ;
        outFile << o[count] << " ";
    }
        cout<< endl;
        outFile << endl;
    } 
    while (next_permutation(o.begin(), o.end()));
}

int main(int argc, char** argv) {

int numofOps;
char Op;
int n;
int r;
string line;

    ifstream myFile("infile.dat");
    myFile >> numofOps;
    myFile.ignore(1,'\n');

    ofstream outFile;
    outFile.open ("example.txt");

    for(int q = 0; q < numofOps; ++q){
    myFile.get(Op);
    myFile >> n;
    myFile>> r;

    vector<string> original(n);
    for(int i = 0;i <= n - 1; i++){
        myFile.ignore(1,'\n');
        myFile >> original[i];}

    myFile.ignore(1,'\n');

    sort(original.begin(), original.end());

    cout<< '\n'<< endl;


    if(Op == 'P'){
        nPermutation(original, r);
    }
    else{
        if(Op == 'C')
            nCombination(original,n,r);
        else
            cout << "Incorrect Input" << endl;
    }
    original.clear();
    }
    outFile.close();
    return 0;
}

My code works but when I add in the outFile to write everything to a file called example, it doesn't work. 我的代码有效,但是当我添加outFile将所有内容写入名为example的文件时,它不起作用。 How can I print out the result of the functions?? 如何打印功能结果?

Examples would be so nice!! 例子太好了!!

One immediate problem springing to mind: 一个紧迫的问题浮现在脑海:

if (bArray[i] == true)
     cout << theVect[i] << " ";
     outFile << theVect[i];

Python is the only language I'm aware of that uses indentation to control blocking of statements. Python是我所知道的唯一使用缩进来控制语句阻塞的语言。 In C-based languages, you should use braces: 在基于C的语言中,应使用花括号:

if (bArray[i] == true) {
     cout << theVect[i] << " ";
     outFile << theVect[i];
}

On top of that, the code you've given shouldn't even compile. 最重要的是,您给出的代码甚至都不应该编译。 You create outFile as a local parameter inside main() then attempt to use it in the other functions, where it's not accessible. 您可以在main() outFile创建为局部参数,然后尝试在无法访问的其他函数中使用它。

You should either make it "global" by moving it out of main() so that every function can see it (unwise, but probably the quickest solution here) or pass it as a parameter to each function that needs to use it. 您应该通过将其移出main()使其成为“全局”,以便每个函数都能看到它(不明智,但这可能是最快的解决方案),或者将其作为参数传递给需要使用它的每个函数。

For the former, you could use: 对于前者,您可以使用:

using namespace std;
ofstream outFile;     // taken from main().

For the latter, it's a matter of adding it for each call to printVector() , nCombination() and nPermutation() , and modifying those functions so that they accept it, for example: 对于后者,只需在每次调用printVector()nCombination()nPermutation() ,然后修改这些函数以使其接受即可,例如:

void nPermutation (
    vector<string> o,
    int            r,
    ofstream       os
) {
    os << "blah blah blah\n";
}
:
nPermutation (original, r, outFile);

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

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