GKED
GKED

Reputation: 410

getline() error

I am writing this function that copies the contents of one file into the other. I am using getline() function in my while loop. 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)"

Upvotes: 1

Views: 9166

Answers (1)

John Harrison
John Harrison

Reputation: 248

One problem is that you've failed to include the <string> header file, which is where getline is defined.

Upvotes: 12

Related Questions