Hi folks,
I'm having a doubt. I write a letter in notepad and i saved it as 'letter.txt'. Then i realized that i forgot to say one matter in that letter. So i opened 'letter.txt' using any text editor such as notepad, wordpad or something. Now i inserted the letters which i want to say in this letter at the middle of the file. My doubt is how it is works ?

example:
this is my message.
" Hi,
How are you. Today i want to
meet you.
Thank you.

it actually stored in memory like this
"Hi,\nHow\0are\0you.\0TodayToday\0i\0want\0to\nmeet\0you.\nThank\0you.
Now i want to add a sentence ' I am fine' after 'How are you. How it is works? How it added in the middle of the file? how the other words are not overwrite. What is process behind it ?

Editors load file contents into memory. How that memory is stored is different for each editor but, for example purposes, you might consider it as a doubly linked list. The cursor is simply sitting at whatever node you are currently at in the list. Adding text on screen is equivalent to inserting more nodes in the linked list. For example:

/* Dashes represent links in the list */
H-o-w- -a-r-e- -y-o-u
              ^ /* cursor position */

If you then type the letters we , you get an update (in memory) of

H-o-w- -a-r-e- -w-e-y-o-u
                   ^ /* cursor position */

This is obviously a simple example but illustrates the mapping from what you see in the editor window to how things may be manipulated in memory.

Editors load file contents into memory. How that memory is stored is different for each editor but, for example purposes, you might consider it as a doubly linked list. The cursor is simply sitting at whatever node you are currently at in the list. Adding text on screen is equivalent to inserting more nodes in the linked list. For example:

/* Dashes represent links in the list */
H-o-w- -a-r-e- -y-o-u
              ^ /* cursor position */

If you then type the letters we , you get an update (in memory) of

H-o-w- -a-r-e- -w-e-y-o-u
                   ^ /* cursor position */

This is obviously a simple example but illustrates the mapping from what you see in the editor window to how things may be manipulated in memory.

can i use this concept to insert a record in the middle of the flat file table? and how ?

If you want to insert data into an existing file you must first determine how the file is stored and then load it into memory. Once you have it in memory you can do what you want with the contents including writing them out to your own format. All of this is non-trivial with anything other than simple text files.

What exactly are you trying to do?

If you want to insert data into an existing file you must first determine how the file is stored and then load it into memory. Once you have it in memory you can do what you want with the contents including writing them out to your own format. All of this is non-trivial with anything other than simple text files.

What exactly are you trying to do?

I want to store a list of records in sorted order in a text( or binary) file. When a new record is to be inserted any where between the existing records, how could i do ?

suppose, for this solution, If i create a temp file and move all the records from the main file to temp file until the new record position which i want to insert is reached and append all the remains. And replace the main file name with the temp file. It is one solution . But if i having large amount of records means, this is gonna be making the process slow . so only i am looking for any other solutions available.

What kind of flexibility do you have in the file format? If it doesn't need to be human readable outside of the reading application, you could store records in the insertion order for a binary search tree. That way there's no mucking about with sorting, it all happens naturally through the tree. Updating the file is a simple matter of overwriting the it with a preorder traversal of the tree.

If you have a really large number of records, you might also consider using a database instead of a flat file for performance.

This article has been dead for over six months. Start a new discussion instead.