You are going to have problems with buffering using your current design. Consider a file composed of the following characters:
H e l l o w o r l d ! \n S a l u t a t i o n s ! \n |
And you wish to replace "world" with "Elizabeth". Writing the replaced line back to file you get:
H e l l o E l i z a b e t h \n u t a t i o n s ! \n |
and the next line you read would be "th".
It would be better to read the
entire file into memory, make your changes there, than overwrite the original file with the new contents. Use a
std::
deque.
Also, there are a few issues you need to be aware of:
It is good that you are using
ios::
binary so that your seeks work properly, but that has the unfortunate effect of removing cross-platform text file newline handling. Reading the file entirely using the default text-mode fixes this problem also.
On line 21 you are inputting a
std::
string using the extraction operator. That's a no-no. Use the
std::
getline() function. (Remember, filenames may have
spaces in them!)
Watch that your commentary is not misleading and that it does not repeat what the code already says. Comments should
explain, not issue a play-by-play.
Make sure to check that things like opening the file worked properly. Also, never loop on EOF when using C++ iostreams. It will cause you problems.
Please don't use system("PAUSE");
I personally
like the EXIT_SUCCESS and EXIT_FAILURE macros, but they are not defined for C++ unless you #include the C standard library... Every C++ program returns zero on success and nonzero on failure, and people will expect seeing that, so it is OK to just use 0 and 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <deque>
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
using namespace std;
int main()
{
deque <string> text; // this is the text of the file we will modify
fstream f; // this is the file stream we'll use
string filename;
string s; // various uses
string s_to_find;
string s_replacement;
cout << "Enter the name of the file: ";
getline( cin, filename );
// Read the file into memory
f.open (filename.c_str(), ios::in);
if (!f.is_open())
{
cout << "Hey! I could not open that file!\n";
return 1;
}
while (getline (f, s))
text.push_back (s);
f.close();
// 'Word find' stuff
cout << "Enter the word to search for (to be replaced): ";
getline (cin, s_to_find);
...
// 'Word find and replace' stuff
cout << "Enter the word you want to replace it with: ";
getline (cin, s_replacement);
...
// Other stuff
...
// Update the file on disk
f.clear();
f.open (filename.c_str(), ios::out | ios::trunc);
for (deque <string> ::const_iterator line = text.begin(); line != text.end(); ++line)
f << *line << "\n";
f.close();
// Keep the console window open long enough to see the output...
cout << "Press ENTER to quit.";
cin.ignore (numeric_limits <streamsize> ::max(), '\n');
return 0;
}
|
Hope this helps.
[edit] Fixed error.