简体   繁体   English

getline() 错误

[英]getline() error

I am writing this function that copies the contents of one file into the other.我正在写这个 function,它将一个文件的内容复制到另一个文件中。 I am using getline() function in my while loop.我在我的 while 循环中使用 getline() function。 Somehow, compiler gives me an error.不知何故,编译器给了我一个错误。 Do you know why?你知道为什么吗? Here is my code:这是我的代码:

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

}

Thank you for your help.谢谢您的帮助。

EDIT: Sorry, here are the errors: 1. " error C3861: 'getline': identifier not found" 2. "error C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)"编辑:对不起,这里是错误:1.“错误C3861:'getline':找不到标识符”2.“错误C2679:二进制'<<':找不到运算符,它采用'std类型的右手操作数: :string' (或者没有可接受的转换)"

One problem is that you've failed to include the <string> header file, which is where getline is defined.一个问题是您未能包含<string> header 文件,这是定义 getline 的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM