0

I have a function that swaps two chars, in a file, at a time, which works, however if i try to use the function more than once the previous swap i made will be wiped from the text file and the original text in now back in, therefore the second change will seem as my first. how can i resolve this?

void swapping_letters()
{
    ifstream inFile("decrypted.txt");   
    ofstream outFile("swap.txt");
    char a;
    char b;
    vector<char> fileChars;

    if (inFile.is_open())
    {
        cout<<"What is the letter you want to replace?"<<endl;
        cin>>a;             
        cout<<"What is the letter you want to replace it with?"<<endl;
        cin>>b;

        while (inFile.good())
        {
            char c;
            inFile.get(c);
            fileChars.push_back(c);
        }                   
        replace(fileChars.begin(),fileChars.end(),a,b);
    }
    else
    {
        cout<<"Please run the decrypt."<<endl;
    }

    for(int i = 0; i < fileChars.size(); i++)
    {
        outFile<<fileChars[i];
    }
}
Dom
  • 59
  • 1
  • 7

1 Answers1

1

What you probably want to do is to parameterize your function :

void swapping_letters(string inFileName, string outFileName)
{
    ifstream inFile(inFileName);
    ofstream outFile(outFileName);
    ...

Because you don't have parameters, calling it twice is equivalent to:

swapping_letters("decrypted.txt", "swap.txt");
swapping_letters("decrypted.txt", "swap.txt");

But "decrypted.txt" wasn't modified after the first call, because you don't change the input file. So if you wanted to use the output of the first operation as the input to the second you'd have to write:

swapping_letters("decrypted.txt", "intermediate.txt");
swapping_letters("intermediate.txt", "swap.txt");

There are other ways of approaching this problem. By reading the file one character at a time, you are making quite a number of function calls...a million-byte file will involve 1 million calls to get() and 1 million calls to push_back(). Most of the time the internal buffering means this won't be too slow, but there are better ways:

Read whole ASCII file into C++ std::string

Note that if this is the actual problem you're solving, you don't actually need to read the whole file into memory. You can read the file in blocks (or character-by-character as you are doing) and do your output without holding the entire file.

An advanced idea that you may be interested in at some point are memory-mapped files. This lets you treat a disk file like it's a big array and easily modify it in memory...while letting the operating system worry about details of how much of the file to page in or page out at a time. They're a good fit for some problems, and there's a C++ platform-independent API for memory mapped files in the boost library:

http://en.wikipedia.org/wiki/Memory-mapped_file

Community
  • 1
  • 1
  • can see where ur coming from, but i running low on time now, and my compilier doesnt like what im doing, meh, so ill just put the code in the program instead of having a function, guess i can swap all the letters i need at once instead of writing to the file each time, thanks tho! – Dom Dec 13 '11 at 17:26