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

C++ problems reading input file and outputting into an output file using getline()

我有一个介绍 C++ 的作业 class,我很困惑为什么我的 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 syntax is incorrect" 那么您将遇到编译失败。由于您的问题没有提到任何编译错误,即使在调查和确定实际问题之前,也可以合理地得出结论:1) getline 语法正确,2) 程序已编译,以及 3) 您需要找到更好的老师,如果您的意图是真正学习 C++,而不受不良导师的阻碍。

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

P.S.:

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

This is a small bug。在这种情况下,错误是隐藏的,因为最终 exit 正确设置了您的示例输入,但您应该在阅读并理解此 link.

中给出的解释后修复此问题