2

私のコード:

void listall()
{
    string line;
    ifstream input;
    input.open("registry.txt", ios::in);

    if(input.is_open())
    {
        while(std::getline(input, line, "\n"))
        {
            cout<<line<<"\n";
        }
    }
    else
    {
        cout<<"Error opening file.\n";
    }
}

私は C++ を初めて使用し、テキスト ファイルを 1 行ずつ出力したいと考えています。Code::Blocks を使用しています。

それが私に与えるエラーは次のとおりです。

エラー: 'getline(std::ifstream&, std::string&, const char [2])' の呼び出しに一致する関数がありません

4

2 に答える 2

4

の有効なオーバーロードは次のstd::getlineとおりです。

istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
istream& getline (istream&  is, string& str);
istream& getline (istream&& is, string& str);

私はあなたが意味したと確信していますstd::getline(input, line, '\n')

"\n"は文字ではなく、サイズが 2 の文字の配列です (1 は の場合'\n'、もう 1 つは NUL-terminator の場合'\0')。

于 2013-07-18T23:28:02.043 に答える
1

これを書きます:

#include <fstream>    // for std::ffstream
#include <iostream>   // for std::cout
#include <string>     // for std::string and std::getline

int main()
{
    std::ifstream input("registry.txt");

    for (std::string line; std::getline(input, line); ) 
    {
         std::cout << line << "\n";
    }
}

エラーチェックが必要な場合は、次のようになります。

if (!input) { std::cerr << "Could not open file.\n"; }
于 2013-07-18T23:27:49.443 に答える