View Single Post
Old 05-09-2005, 01:18 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is online now   Reply With Quote