![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 1
| How to replace words in a text file with seekp ex. [file.txt] I have five fingers. My current code: ofstream File; File.open("file.txt", ios: ut);File.seekp(8, ios::beg); File << "four"; File.close(); I have tried different combinations of ios on the second line, but it either a) erases the whole file and writes "four" at the begining or end b) writes "four" at the very end ("I have five fingers.four") c) erases the whole file and writes "four" eight characters into the file I know I can get it to work by reading in the file and going through it word by word, then re-outputting the right words and replace then at the correct time, but the actual file is extremely large so processing the whole file is not an option. Assuming I know the starting byte locations of everything I want to change, byte 8 in this case, how can I get the file.txt to display this: I have four fingers Is there any way to do this and avoid using ios::binary? I've tried changing File << "four"; to File.write("four", 8); but it just writes "four" eight characters into the file and nothing else is there. Thanks |
| ishould is offline | |
| | #2 |
| Registered User Join Date: Dec 2006 Location: Scranton, Pa
Posts: 221
| Could mess with what's below; Code: #include <fstream>
int main()
{
fstream fout("changeme.txt", ios::in |ios::out |ios::beg);
fout.seekg(7, ios::beg);
fout<<"four"<< " ";
fout.close();
return 0;
}
|
| Oldman47 is offline | |
![]() |
| Tags |
| binary, ios, replace, seekp |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Storing Words from a text file | matt_570 | C++ Programming | 18 | 12-10-2008 12:35 PM |
| Memory Address | kevinawad | C++ Programming | 18 | 10-19-2008 10:27 AM |
| Removing text between /* */ in a file | 0rion | C Programming | 2 | 04-05-2004 08:54 AM |
| random selection of words from a text file | archie | C++ Programming | 0 | 03-02-2002 12:59 AM |