getline() error

getline() error

本文关键字:error getline      更新时间:2023-10-16

我正在编写这个函数,它将一个文件的内容复制到另一个文件中。我在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:二进制'<<':找不到接受右操作数'std::string'类型的操作符(或者没有可接受的转换)"

一个问题是您没有包含<string>头文件,这是定义getline的地方。