【问题标题】:Quick Q - Can't Write to File the Output of a FunctionQuick Q - 无法写入函数的输出文件
【发布时间】:2014-12-05 02:57:40
【问题描述】:

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;
}

我的代码有效,但是当我添加 outFile 以将所有内容写入名为 example 的文件时,它不起作用。如何打印出函数的结果??

例子太好了!!

【问题讨论】:

  • 您无法从另一个函数访问outFile,因为它的本地范围为main。要么将其作为另一个参数传递,要么将 outFile 设为全局 - 后者不太可取。
  • 即使我将其设为全局,它也会编译但没有任何内容写入文件?有什么帮助吗??

标签: c++ function ofstream


【解决方案1】:

突然想到的一个问题:

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

Python 是我所知道的唯一一种使用缩进来控制语句阻塞的语言。在基于 C 的语言中,您应该使用大括号:

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

最重要的是,您给出的代码甚至不应该编译。您在 main() 中创建 outFile 作为 local 参数,然后尝试在其他无法访问的函数中使用它。

您应该通过将其移出main() 使其成为“全局”,以便每个函数都可以看到它(不明智,但可能是这里最快的解决方案),或者将其作为参数传递给需要使用它的每个函数.

对于前者,您可以使用:

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

对于后者,只需在每次调用 printVector()nCombination()nPermutation() 时添加它,并修改这些函数以便它们接受它,例如:

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

【讨论】:

  • 即使我将其设为全局,它也会编译但没有任何内容写入文件?有什么帮助吗??
  • 你还记得在 main 中打开它吗?
  • 在函数内打开?
  • @Not_NSA_I_Swear,不,仍然打开它。您可能需要检查outFile.fail() 以查看打开文件是否有问题。并确保您已将声明从main移动到全局区域,而不是复制它。否则,您打开主本地的并尝试在其他功能中使用未打开的全局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
相关资源
最近更新 更多