![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
Problem inserting (appending) in the middle of text fileHi,
I have a C++ coding which prompts user to enter a username and password which is then compared with username,password bank account no and bank balancestored in a text file (new.txt).If the username and passowrd is correct then the user account no and balance are displayed. After which I replace the balance in the text file with a string str3="51907".I use the function seekg to point to the location from where i need to write and then clear the previous value (balance) by writing blankspace (" ").After which I write the string str3 again using seekg() function to point to the exact location. Now the problem is sometimes if the string (str3) is larger or longer in length when I write this value to the text it overwrites on to the next character (next line).In other words I am not able to append from middle of text without erasing the next character in text file (if string exceeds size). My code: [ CPP / C++ / C Code:
new.txt (file) looks like this: Quote:
If I happen to insert/append this way the string (str3) sometimes overwrites to next immediate character in file.In other words the next character gets erased.Any ideas on avoiding overwriting next character and simply append?... Help appreciated Regards, Rajeev |
|||
|
#2
|
|||
|
|||
Re: Problem inserting (appending) in the middle of text fileQuote:
The way to do it is: 1. Open Original File. 2. Open a brand New File. 3. Read things from the Old File and write into the New File up to the point right before the place for your replacement text. 4. Read and discard the items to be replaced from the Old File. Write the new data to the New File. 5. If there are additional new items to be written, then repeat steps 3 and 4 until there are no more new items. 6. Read everything else from the Old File from the Old file and write them into the New File until the Old File has nothing else to read. 7. Rename the Old File to some name that indicates it is a backup file. (If you don't want to keep backup file, then just delete it. 8. Rename the New File to whatever name the Original File had. Regards, Dave |
|
#3
|
|||
|
|||
Re: Problem inserting (appending) in the middle of text fileIf you are in control of the format of the text file, make each item a fixed width and pad with blanks. That way you can overwrite in the middle of the file without messing up other lines.
|
|
#4
|
|||
|
|||
Re: Problem inserting (appending) in the middle of text fileHi,
Thanks for all those responses.I think I have found a possible solution If I happen to use "\n" at the end of string (by string concatenation) which I am inserting in the middle of a text file I was able to avoid overwriting of the next immediate string as this string is shifted to a new line. My idea is to build a database (for a bank) which keeps on updating the user's financial transactions (credits/debits).I was able to achieve what I needed by using "\n" new line character symbol (only if the string exceeded size) while writing into the file thereby avoid overwriting of next character (if string length exceeded previous length) in the text file. Thank You, Regards, Rajeev Nair. |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CD Burner Help - Power Calibration Error.... | JonBoy420 | Computer Hardware Forum | 111 | 19-Feb-2009 03:54 |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| Help! Problems encountered while burning CD's | RayDarkness | Computer Software Forum - Windows | 1 | 18-Nov-2006 04:54 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The