This is your problem
Code:
while(file.eof()==0)
{
file.get(ch);
if(ch=='h')
ch='i';
}
You identify the char correct, but once you've set ch to teh char you want to change it to, then you read past the point to place it.
A way of achieving it, would be to keep an array of positions, where the char was found, then in the end search through the file and replace the chars at the previus located postions.
Or you could simply replace it in the file, when ever you encounter it.
Something like this:
Code:
#include <iostream>
#include <fstream>
int main()
{
char ch;
fstream file("m.txt",ios::in|ios::ate|ios::out);
file.seekg(0);
while(file.eof()==0)
{
file.get(ch);
if(ch=='h')
{// seek back one char and replace it
file.seekp(file.tellg() - sizeof(char));
file << 'i';
}
}
file.close();
return 0;
}
Note: none of this code has been tested.