0

タイトルが少し漠然としていることは承知していますが、今はこれ以上のタイトルが思い浮かびません。私のコードからの抜粋は次のようになります。

#include<iostream>
#include<fstream>
int main(){
ifstream f("cuvinte.txt");
f.getline(cuvant);
return 0;
}

「cuvinte.txt」から次の単語を読みたいときは、 f.getline(cuvant); と書きます。しかし、私は次のエラーが発生します

error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments

何が問題なのかわからず、しばらく前にこの問題に出くわしましたが、まだそれを乗り越えることができません。

4

4 に答える 4

5

何が問題なのかわからず、しばらく前にこの問題に出くわしましたが、まだそれを乗り越えることができません。

参考までに!

basic_istream& getline( char_type* s, std::streamsize count );

サイズ、つまり で利用可能なスペースの量を指定する必要がありますcuvant

f.getline(cuvant, size);
                  ^^^^

編集

別の方法として、より最新の楽器を使用することもできます。

string cuvant;
getline(f, cuvant);
于 2012-08-26T16:07:49.020 に答える
1

あなたは、さまざまな形式の getline に慣れていることに少し不安を感じているようです。参考までに、いくつかの簡単な使用方法を次に示します。

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

using namespace std;

int main()
{
    string filepath = "test.txt";               // name of the text file
    string buffer;                              // buffer to catch data in
    string firstLine;                           // the first line of the file will be put here

    ifstream fin;

    fin.open(filepath);                         // Open the file
    if(fin.is_open())                           // If open succeeded
    {
        // Capture first line directly from file
        getline(fin,firstLine,'\n');            // Capture first line from the file.
        cout << firstLine << '\n';              // Prove we got it.

        fin.seekg(0,ios_base::beg);             // Move input pointer back to the beginning of the file.

        // Load file into memory first for faster reads,
        // then capture first line from a stringstream instead
        getline(fin,buffer,'\x1A');             // Capture entire file into a string buffer
        istringstream fullContents(buffer);     // Put entire file into a stringstream.
        getline(fullContents,firstLine,'\n');   // Capture first line from the stringstream instead of from the file.
        cout << firstLine << '\n';              // Prove we got it.

        fin.close();                            // Close the file
    }



    return 0;
}

次のサンプル ファイルを使用します。

This is the first line.
This is the second line.
This is the last line.

次の出力が得られます。

This is the first line.
This is the first line.
于 2012-08-26T16:51:42.150 に答える
0

のプロトタイプは次のgetlineとおりです。

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

そのため、エラー メッセージに明確に示されているように、1 つの引数で呼び出すことはできません...

于 2012-08-26T16:08:03.703 に答える
0

が であると仮定するcuvantstd::string、正しい呼び出しは

std::getline(f, cuvant);
于 2012-08-26T16:14:58.033 に答える