【问题标题】:getline() errorgetline() 错误
【发布时间】:2011-07-12 20:04:07
【问题描述】:

我正在编写这个函数,它将一个文件的内容复制到另一个文件中。我在我的 while 循环中使用 getline() 函数。不知何故,编译器给了我一个错误。你知道为什么吗? 这是我的代码:

#include<iostream>
#include<cstdlib>
#include <fstream>

using namespace std;

// Associate stream objects with external file names

#define inFile "InData.txt" // directory name for file we copy from
#define outFile "OutData.txt"   // directory name for file we copy to

int main(){
    int lineCount;
    string line;
    ifstream ins;   // initialize input object an object
    ofstream outs;  // initialize output object
    // open input and output file else, exit with error

    ins.open("inFile.txt");
    if(ins.fail()){
        cerr << "*** ERROR: Cannot open file " << inFile
            << " for input."<<endl;
        return EXIT_FAILURE; // failure return
    }

    outs.open("outFile.txt");
    if(outs.fail()){
        cerr << "*** ERROR: Cannot open file " << outFile
            << " for input."<<endl;
        return EXIT_FAILURE; // failure return
    }

    // copy everything fron inData to outData
    lineCount=0;
    getline(ins,line);
    while(line.length() !=0){
        lineCount++;
        outs<<line<<endl;
        getline(ins,line);
    }

    // display th emessages on the screen
    cout<<"Input file copied to output file."<<endl;
    cout<<lineCount<<"lines copied."<<endl;

    ins.close();
    outs.close();
    cin.get();
    return 0;

}

感谢您的帮助。

编辑:对不起,这里是错误: 1.“错误C3861:'getline':找不到标识符” 2.“错误C2679:二进制'

【问题讨论】:

  • 不要让我们猜测编译器错误
  • 确切的编译器错误是什么?在哪一行?
  • 什么错误和什么代码行?我用 ideone 试了一下,它似乎编译得很好:ideone.com/BO7tY

标签: c++ ifstream getline


【解决方案1】:

一个问题是您未能包含 &lt;string&gt; 头文件,这是定义 getline 的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 2013-12-13
    • 2021-12-31
    • 2014-04-26
    • 1970-01-01
    • 2012-11-07
    • 2011-07-02
    • 2011-10-07
    相关资源
    最近更新 更多