C++问题读取输入文件并使用getline()输出到输出文件

标签 c++ file-io getline shapes

我有一个 C++ 入门课的作业,我很困惑为什么我的 getline 似乎可以工作,但它没有将我的函数输出到 outfile.txt 中。我的老师说我的 getline 语法不正确,但我对如何操作感到困惑。 我的 infile.txt 内容如下:

T & 4
S @ 6
T x 5
R * 5 7
D $ 7
D + 5
R = 4 3
E

还有我的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;


void draw_rect (char out_char, int rows, int columns); // Draws a rectangle shape

void draw_square (char out_char, int rows); //Draws a square shape

void draw_triangle (char out_char, int rows);// Draws a triangle shape

void draw_diamond (char out_char, int rows); // Draws a diamond shape

int main()
{
    ofstream outfile;
    ifstream infile;
    int row, col;
    bool exit = false;
    char value;
    infile.open("infile.txt");
    outfile.open("outfile.txt");
    if(!infile.good())
    {
        cout << "failed to open\n";
    }else
    {
        string buffer;
        while(!infile.eof() || !exit)
        {
            getline(infile, buffer);
                switch(buffer[0])
                {
                case 'R':
                   value = buffer[2];
                   row = buffer[4];
                   col = buffer[6];
                   draw_rect(value, row, col);
                   break;
                case 'T':
                    value = buffer[2];
                    row = buffer [4];
                    draw_triangle(value, row);
                    break;
                case 'D':
                    value = buffer[2];
                    row = buffer[4];
                    draw_diamond(value, row);
                    break;
                case 'S':
                    value = buffer[2];
                    row = buffer[4];
                    draw_square(value, row);
                    break;
                case 'E':
                    cout << "Files Written.\nExiting." << endl;
                    exit = true;
                    break;
                default:
                    cout << "Invalid input, try again" << endl;
                }
        }
    }
    return  0;

}

void draw_diamond (char out_char, int rows)
{
    ofstream outfile;
    int space = 1;
    space = rows - 1;
    for (int i = 1; i <= rows; i++)
    {
        for (int k = 1; k <= space; k++)
        {
            outfile << " ";
        }
        space--;
        for( int k = 1; k <= 2*i-1; k++)
        {
            outfile << out_char;
        }
        outfile << endl;
    }
    space = 1;
    for (int i = 1; i <= rows; i++)
    {
       for(int k = 1; k <= space; k++)
       {
           outfile << " ";
       }
       space++;
       for(int k = 1; k <= 2*(rows-i)-1; k++)
       {
           outfile << out_char;
       }
       outfile << endl;
    }
}

void draw_triangle (char out_char, int rows)
{
    ofstream outfile;
     for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            outfile << out_char;
        }
        outfile << endl;
}
}

void draw_square (char out_char, int rows)
{
    ofstream outfile;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < rows; j++)
        {
            outfile << out_char;
        }
        outfile << endl;
    }
}

void draw_rect (char out_char, int rows, int columns)
{
    ofstream outfile;
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            outfile << out_char;
        }
        outfile << endl;
    }
}

最佳答案

以您的一个函数为例:

void draw_rect (char out_char, int rows, int columns)
{
    ofstream outfile;

这会在您的 draw_rect() 函数中声明一个新的 std::ofstream 对象。 draw_rect() 不会打开此 std::ofstream 对象,因此此 draw_rect() 尝试将任何内容写入此 outfile 什么都不做。

您在 main() 中创建了一个具有相同名称的 std::ofstream 对象这一事实,您确实打开了,这绝对没有任何意义。仅仅因为您在不同的函数中拥有同名的同一个对象并不意味着它们是同一个对象。 C++ 不是这样工作的。

您需要更改 main() 函数以通过引用将其 outfile 作为参数传递给每个函数,而不是声明另一个具有相同名称的对象在每个函数中。上面的函数是,例如:

void draw_rect (ofstream &outfile, char out_char, int rows, int columns)
{

您将需要更改每个函数以通过引用接收 outfile 参数,像这样,更改它们的前向声明,并显式传递 outfile 作为第一个来自 main() 的参数,在每个函数调用中。

最后,关于您声称您的老师告诉您您对 getline 的使用有问题:

您的困惑是完全合理的。如果您的“getline 语法不正确”,那么您将遇到编译失败。由于您的问题没有提到任何编译错误,即使在调查和确定实际问题之前,也可以合理地得出结论:1) getline 语法正确,2) 程序已编译,以及 3) 您需要找到更好的老师,如果您的意图是真正学习 C++,而不受不良导师的阻碍。

从各方面来看,您的问题与 getline 无关。您的老师简直无能,对 C++ 的了解也不多。当然,这不是你的错,但你需要明白这一点。

附言:

while(!infile.eof() || !exit)

This is a small bug .在这种情况下,错误是隐藏的,因为最终 exit 正确地设置了您的示例输入,但您应该在阅读并理解此链接中给出的解释后修复此问题。

关于C++问题读取输入文件并使用getline()输出到输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55192703/

相关文章:

C++ 类 - 指针问题

C++ std::string::assign 工作起来很奇怪

c++ - printf double 最多可显示 n 位小数,但根据需要不能更多

python - 使用 python 以相同的速率迭代文本文件中的列表和行?

java - 生成的 CSV 文件中不需要的双引号

python - 提取 = 和 ; 之间的整数

c++ - GLSL布局std140填充困境

c++ - 为什么 std::getline 在使用 operator>> 两次后失败?

C++:字符串中的空格阻止我比较 cin 输入。

c++ - std::getline 在遇到 eof 时抛出