C++/C++ help


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

Or even:

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.  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.

[an error occurred while processing this directive]