ageoff
ageoff

Reputation: 2828

C++: Reverse the lines in a text file using stack

I have this code so far:

#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main () {


ifstream in;
in.open("example.txt");

ofstream outfile;
outfile.open("out.txt");

stack<string> lines;
string temp;
while(getline(in, temp))
    lines.push(temp);
while(!lines.empty())
    outfile << lines.pop() << endl;

in.close();
outfile.close();

return 0;
}

My question is, why did i get a compile error of "no match for operator << in outfile".

Upvotes: 1

Views: 1838

Answers (1)

hmjd
hmjd

Reputation: 122001

pop() returns void, not a std::string. Use top() and then pop():

while(!lines.empty())
{
    outfile << lines.top() << endl;
    lines.pop();
}

Upvotes: 7

Related Questions