【问题标题】:Inputting into files and outputting to another file and passing ofstream files to functions输入文件并输出到另一个文件并将流文件传递给函数
【发布时间】:2020-02-25 15:08:55
【问题描述】:

Pretext: 我是 C++ 编码的新手,并且知道数组和字符串。这位教授演示了如何输入和输出到文件中,并给了我们以下作业。

作业题:在文件中输入一个数字。现在想象有一个带有输入步数的楼梯。您可以上一步并用“u”表示,也可以上两步并用“d”表示。找出这种类型的所有可能组合并将它们写入一个文件。

示例: 对于 n=1,输出应为 'u';对于 n=2,输出应该是 'uu d';对于 n=3,输出应该是 'uuu ud du' 等等。

我的思考过程:所以这基本上是一个斐波那契数列。所以就像斐波那契数列一样,我想到了一个递归算法。这是我想出的......

代码

#include<iostream>
#include<fstream>
using namespace std;
void staircase(int num, int count, char* c, int index, ofstream &file)
{
    char c1[num], c2[num];    
    for (int i = 0;i < num;i++) {
        c1[i] = c[i];
        c2[i] = c[i];
    }
    if (num - count == 2) {
        c1[index] = 'd';
        c2[index] = 'u';
        c2[index + 1] = 'u';
        file.open("output.txt", ios::app);
        file << c1 <<"  ";
        file << c2 <<"  ";
        file.close();
        return;


    }
    if (num - count == 1) {
        c2[index] = 'u';
        file.open("output.txt", ios::app);
        file << c2 <<"  ";
        file.close();
        return;

    }
    if (num - count > 2) {
        c1[index] = 'd';
        c2[index] = 'u';
        staircase(num, count + 2, c1, index+1,file);
        staircase(num, count + 1, c2, index+1,file);
    }
}
int main(){
    int num;
    cout<<"Input total number of stairs: ";
    cin>>num;
    fstream myfile;
    myfile.open("input.txt");
    myfile<<num;
    myfile.close();
    cout<<"Input is saved in file Directory   ";
    char c[num];
    ofstream file;
    file.open("output.txt");
    staircase(num, 0, c, 0, file);
}

问题:当我在没有文件代码的情况下编写它时,它一切正常,命令提示符显示所有可能的输出。我还注意到对于 n=1 和 n=2 它甚至不会在文件上打印任何内容。我觉得我错过了 fstream 的一些东西,无法指出它。我试图搜索谷歌和stackoverflow。谢谢你帮助我。以下是没有任何文件的版本。

#include<iostream>
#include<fstream>
using namespace std;
void staircase(int num, int count, char* c, int index)
{
    char c1[num], c2[num];    
    for (int i = 0;i < num;i++) {
        c1[i] = c[i];
        c2[i] = c[i];
    }
    if (num - count == 2) {
        c1[index] = 'd';
        c2[index] = 'u';
        c2[index + 1] = 'u';
        for(int i=0;i<=index;i++)
        cout<<c1[i];
        cout<<" ";
        for(int i=0;i<=index+1;i++)
        cout<<c2[i];
        cout<<" ";
        return;


    }
    if (num - count == 1) {
        c2[index] = 'u';
        for(int i=0;i<index+1;i++)
        cout<<c2[i];
        cout<<" ";
        return;

    }
    if (num - count > 2) {
        c1[index] = 'd';
        c2[index] = 'u';
        staircase(num, count + 2, c1, index+1);
        staircase(num, count + 1, c2, index+1);

    }
}
int main(){
    int num;
    cout<<"Input total number of stairs: ";
    cin>>num;
    cout<<"Input is saved in file Directory   ";
    char c[num];
    staircase(num, 0, c, 0);
}

【问题讨论】:

  • 我的输出文件中缺少值。
  • staircase 中删除所有使用openclose。该文件已经打开,当main完成时会自动关闭。
  • @molbdnilo 对于 n=2 它正在返回 'd uuo ',对于 n=1 它正在返回 'uúo '。我以前试过这个,但得到了额外的字符。但是对于 n>1 它工作正常,我刚刚检查过。
  • 你的函数并不相同——你出于某种原因删除了一些循环——所以结果不同也就不足为奇了。取您的“cout 版本”,添加一个ostream &amp;file 参数,并在函数中将cout 替换为file。 (使用ostream&amp; 而不是ofstream&amp; 可以让您将coutmain 传递给函数,并在不使用文件的情况下验证输出。)
  • 您的问题似乎是关于如何读取/写入文件。但是那里有很多关于你的学校作业与问题无关的内容。请阅读如何制作minimal reproducible example

标签: c++ fstream ifstream ofstream


【解决方案1】:

在您对问题的描述中,流式传输到std::cout 的版本听起来可以正确完成工作。在这种情况下,只需将参数添加到 staircase 并采用 std::ostream&amp; 并将函数中的 cout 替换为 ostream 参数即可。这样你就可以同时流式传输到std::cout 和打开std::ofstreams。

如果您想继续使用标准 C++,请不要使用 VLA:s - 请改用 std::vector&lt;&gt;。它有很多不错的功能,例如一个名为 size() 的成员函数,您可以使用它,而不必将 num 作为一个单独的参数与您的 VLA 一起传递。

例子:

#include <cstddef>
#include <fstream>
#include <iostream>
#include <vector>

// When using a vector, you don't need to pass in "num". The vector
// has a size() function that returns how many enements it contains.
// Also note the added std::ostream parameter for streaming to any ostream.
void staircase(size_t count, const std::vector<char>& c, size_t index, std::ostream& os)
{
    std::vector<char> c1 = c; // create a copy of c instead of manual looping
    std::vector<char> c2 = c; // create a copy of c instead of manual looping

    if(c.size() - count == 2) {
        c1[index] = 'd';
        c2[index] = 'u';
        c2[index + 1] = 'u';
        for(size_t i = 0; i <= index; ++i) os << c1[i];
        os << ' ';
        for(size_t i = 0; i <= index + 1; ++i) os << c2[i];
        os << ' ';
        return;
    }
    if(c.size() - count == 1) {
        c2[index] = 'u';
        for(size_t i = 0; i < index + 1; ++i) os << c2[i];
        os << ' ';
        return;
    }
    if(c.size() - count > 2) {
        c1[index] = 'd';
        c2[index] = 'u';
        staircase(count + 2, c1, index + 1, os);
        staircase(count + 1, c2, index + 1, os);
    }
}

int main() {
    size_t num;
    std::cout << "Input total number of stairs: ";
    std::cin >> num;

    // use a vector instead of a VLA:
    std::vector<char> c(num, 0);

    // Just call staircase with an open file instead of std::cout
    // if you want the output to a file.
    staircase(0, c, 0, std::cout);

    std::cout << '\n';
}

【讨论】:

  • 感谢您的回答。好吧,感谢您提出。我会努力解决的。我相信你的方式可以完成这项工作。我能问点什么吗?我在哪里可以学习高级 C++ 的东西?我想我大部分的算法都是正确的,我可以做基本的编程没问题。我想学习高级概念及其实现。
  • 不客气!大多数人建议获得a good book 或两个。就我个人而言,我对书籍不是很好,所以我只是继续我开始的方式,尝试一些有趣的东西。我敢肯定,我的方式比阅读要花更长的时间......
  • 是的。我的方式也是一样的,我从来没有读过关于编程的书,但我觉得 stackexchange 中的人写了很多不同和先进的代码。它渴望我变得更好,因此这是我的问题。我想我也会慢慢学习的。
  • @Rio 是的,stack* 确实是一个很好的灵感来源。自从我开始经常光顾 SO 以来,我觉得我学到了很多东西——通过尝试理解其他人的答案和尝试自己解决人们的问题。它正在阅读 - 但没有涉及任何书籍:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多