Counting lines from file and diplaying them in reverse order

I want to count the lines from input.txt and to display the lines in the output text in reverse order.

1
2
3
4
 input.txt    output1.txt  output2.txt
 Line 1        Line 3          3
 Line 2        Line 2
 Line 3        Line 1


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <fstream>
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
	ifstream fin("input.txt");
	ofstream fout("output.txt");

	int i;
	char S[100];

	//Reading the file
	while(!fin.eof())
	{
		fin.getline(S, 100);
	}
	
	//Counting the lines of the file
	int s=0;  //initializing the sum
	for(i=0; S[i]; i++)
	{
	
		if(S[i]=='\n') //everytime we meet the escape character
		s++;           // increment the sum
	}
	fout << s; // s=0, why?

       //Displaying the lines in reverse order... any ideas?
}
fout << s; // s=0, why? because std::istream.getline() uses the '\n' character as a delimiter and does not save it to the string
Topic archived. No new replies allowed.