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 );

所以,正如错误信息明确指出的那样,你不能用一个参数来调用它......

于 2012-08-26T16:08:03.703 回答
0

假设cuvantstd::string,正确的调用是

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