c++ - 获取线();使用 C++ 时出错

标签 c++ string getline

当我尝试运行以下程序时,我从 getline() 调用中收到以下错误。

信息是:

In function 'int main()':
error: no matching function for call to 'getline(std::ofstream&, std::string&)'

我不知道为什么会得到这个 - 我有 string 库。

我的代码:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int NumeroLetras;

    cout << "Juego de El Ahorcado\n--------------------\n" << endl;

    cout << "N\243mero de letras de la palabra: ";
    cin >> NumeroLetras;

/*                Creamos el fichero con las palabras a adivinar
------------------------------------------------------------------------*/
    cout << "Creando fichero con palabras..." << endl;
    ofstream fichero("palabras.txt");
    fichero << "baloncesto\n";
    fichero << "beisbol\n";
    fichero << "futbol\n";
    fichero << "golf\n";
    fichero << "rugby\n";
    fichero << "tenis\n";
    fichero << "boxeo\n";
    fichero << "sumo\n";
    fichero << "judo\n";
    fichero << "nascar\n";
    fichero << "atletismo\n";
    fichero << "caminata\n";
    fichero << "ciclismo\n";
    fichero << "esgrima\n";
    fichero << "natacion\n";
    fichero << "polo\n";
    fichero << "clavados\n";
    fichero << "remo\n";
    fichero << "vela\n";
    fichero << "ajedrez\n";
    fichero.close();
    cout << "Fichero creado exitosamente..." << endl;

/*                Determinamos el tamaño de las palabras
------------------------------------------------------------------------*/
    string Palabra;

    fichero.open("palabras.txt");

    while (! fichero.eof())
    {
        getline(fichero, Palabra);

        cout << Palabra << endl;
    }

    return 0;
}

为什么会出现该错误?

最佳答案

您正在尝试从 ofstream(输出 文件流)中读取

创建一个新的 ifstream 变量并从中读取。

关于c++ - 获取线();使用 C++ 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33482902/

相关文章:

c - 查找以另一个文本开头的文本

python - Pandas:从带有字符串标题的列中删除某些值

c++ - Getline 无法按预期处理字符串

c++ - 将动态创建的对象分配给作为类的私有(private)成员的指针有什么问题吗?

c++ - 指向常量数组的指针的正确定义是什么?

c - 即使要查找的字符串位于索引 0,我的 strstr() 也返回 null

c++ - 如何使用嵌套的 getline() 函数来获取字符串中的特殊字符和标点符号?

c++ - 将一个 header 包含到我的 MVS 项目时出错

c++ - 使用 cygwin 在 64 位 Windows 上的 C++ 中 int 指针的大小

c++ - 在 C++ 中逐行读取二进制文件