i have a input text file with a phase in it and i am trying to change the word 'be' to the word 'there'. i need to take into account the white spaces i am guessing. should i use strings or is there an easier way to do this??
I am going to assume this is some sort of homework, coursework, course assignment or course project work etc. As such I am not going to go into too much specific detail as it is your assignment and too much detail will effectively have done it for you.
Well the only way to change something in a file is to write the new data to it. If that data is dependent on existing data then you have to read the existing data, modify it and write it back.
If the data in the file is text data then reading it as one or more strings of characters is appropriate. Once you have the data as a string you can then modify the string as appropriate - in this case replacing the word "be" with the word "there". Replace is the operative word here as the C++ library class std::string contains various replace operations, however taking into account all possible situations involving white space and occurrences at the beginning and end of the phrase will add complexity. This complexity will increase even further if you have to take into account upper and lower case variations.
I suggest (as always) that you start simply and build on the complexity. I would expect most of the modifications would be in the area of modifying the phrase string.
So the outline of your program is:
Read phrase from file to string
Modify phrase string replacing "be" with "there".
Write modified phrase string to file
There are some things to note about this procedure:
The std::istream::operator>> for extracting strings will extract a word at a time (i.e. characters surrounded by white space). This could be used to simplify your modification logic by reading, modifying if necessary, and writing the phrase one word at a time. In fact you could even just replace the read word with its replacement:
If word is "be" then assign "there" to word
If word is "be" then write "there" else write word
However you will loose information about the exact type and quantity of white space around each word in the simplest implementation, which may not be acceptable. You would presumably write the white space between the words yourself as say a single space.
You do not say whether you are required to write the modified phrase back to the same file or a different file. If you are writing the modified phrase to a different file then you can just open one for reading and the other for writing and read original phrase data from one and write the modified phrase data to the other.
If you are overwriting the same file then easiest way to proceed is to open the file for reading, read and modify the phrase and then close and open the file for writing (truncating any exiting data) and writing the new phrase to the file. This assumes the phrase is the only data in the file. As this destroys the original data I suggest you keep a master copy of the original data that you can copy to the data file after each execution of the program. That way you will not have to keep typing out the initial data into the data file each time you test your code!
An alternate approach is to write to a separate file, then rename the original and the new file so the new file has the original file's name and the original is now a backup file, you can then delete the backup file. However this requires using non-standard C/C++ library function and/or operating system specific functions.
Yet another approach would be to open the file for reading and write-appending. You can then read the phrase, re-position the file put position to the beginning of the phrase and modify and write the phrase back to the file.
One thing to note is that the word "there" is longer than the word "be". Therefore if the phrase is not the last item in the file you may have to read and rewrite all data after the phrase otherwise you might overwrite part of the remaining data. For example, given the source text:
"If be twenty make it forty. Therefore I cannot go with you."
The phrase consists of the first 6 words and the first full stop (i.e. the first sentence). If you used the last method to process the file (open for reading and writing and repositioning the file put position) then if you overwrite the first sentence with:
"If there twenty make it forty."
It is three characters longer and will overwrite the following three characters (a space a 'T' and an 'h') so the file will read:
"If there twenty make it forty.erefore I cannot go with you."
Hope this gives you some hints.