1

我正在编写这个函数,它将一个文件的内容复制到另一个文件中。我在我的 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' (或者没有可接受的转换)"

4

1 回答 1

12

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

于 2011-07-12T20:16:22.437 回答