working with file

Hi
In this code i should reverse the things that are in input.txt and put them in output.txt for example if in input i have 123 in out put should become 321 and i want to do it with seekp and seekg so please tell me how should i correct this code so that it gives the correct result
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
#include<iostream>
#include<fstream>

using namespace std;

int main()
{
	char c;
	int i;
	ifstream fin("input.txt");
	ofstream fout("output.txt");
	for(i=0;fin.eof()==false;i++)
	{
		fin.get(c);
		fin.get();
	}
	i=i-2;
	fin.seekg(0,ios::beg);
	while(i>=0)
	{
		fin.get(c);
		fout.seekp(i);
		fout.put(c);
		cout<<c<<endl;
		i=i-2;
	}
	return 0;
}

Last edited on
sorry but i need the answer fast please someone help
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
33
34
#include <iostream>
#include <fstream>
using namespace std;

int main () {
  int length;
  
char x;
  ifstream is;
  is.open ("input.txt" );

 
  is.seekg (0, ios::end);
  length = is.tellg();
  
is.seekg (length-2, ios::beg);
 
  is >> x;
  cout << x <<endl;
is.seekg (length-3, ios::beg);
 
  is >> x;
  cout << x <<endl;
is.seekg (length-4, ios::beg);
 
  is >> x;
  cout << x <<endl;


  is.close();

 
  return 0;
}

thanks for your answer but i dont understand what happened on output.txt?
I didn't do that. I put them to the console. You'll have to look at the code and work out how to change it yourself.
Thank you very much it works now
Topic archived. No new replies allowed.