7 Replies - 3650 Views - Last Post: 08 June 2012 - 11:52 PM Rate Topic: -----

#1 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Write to the middle of a file

Posted 05 June 2012 - 10:29 AM

Hello DIC. I'm currently working on a project where I want to print usernames/passwords to a file and have them organized alphabetically by the username. I know there is a pointer to a current line somewhere in fstream but is there a way to set that to a specific line?

Thanks in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: Write to the middle of a file

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4098
  • View blog
  • Posts: 12,681
  • Joined: 25-December 09

Re: Write to the middle of a file

Posted 05 June 2012 - 10:33 AM

You will need to do the "organizing" in memory before you write to your file. Then write the "organized" information to your file.

Jim
Was This Post Helpful? 2
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Write to the middle of a file

Posted 05 June 2012 - 11:09 AM

View Postnunc, on 05 June 2012 - 10:29 AM, said:

Hello DIC. I'm currently working on a project where I want to print usernames/passwords to a file and have them organized alphabetically by the username. I know there is a pointer to a current line somewhere in fstream but is there a way to set that to a specific line?

Thanks in advance.


You are free to set the file pointer at any byte offset in the fstream that you like, but it is up to you to figure out whether that offset is at a specific line or not. The "figuring out" tends to get complex, so it'll be easier to follow Jim's advise on post #2.

This post has been edited by Skydiver: 05 June 2012 - 11:11 AM

Was This Post Helpful? 2
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Write to the middle of a file

Posted 05 June 2012 - 11:51 AM

http://webster.cs.uc...TML/Files3.html

You might like to also see the article at section ...

7.5 ISAM (Indexed Sequential Access Method) Files

ISAM is a trick that attempts to allow random access to variable-length records in a sequential file. This is a technique employed by IBM on their mainframe data bases in the 1960's and 1970's. Back then, disk space was very precious (remember why we wound up with the Y2K problem?) and IBM's engineers did everything they could to save space. At that time disks held about five megabytes, or so, were the size of washing machines, and cost tens of thousands of dollars. You can appreciate why they wanted to make every byte count. Today, data base designers have disk drives with hundreds of gigabytes per drive and RAID1 devices with dozens of these drives installed. They don't bother trying to conserve space at all ("Heck, I don't know how big the person's name can get, so I'll allocate 256 bytes for it!"). Nevertheless, even with large disk arrays, saving space is often a wise idea. Not everyone has a terabyte (1,000 gigabytes) at their disposal and a user of your application may not appreciate your decision to waste their disk space. Therefore, techniques like ISAM that can reduce disk storage requirements are still important today.

ISAM is actually a very simple concept. Somewhere, the program saves the offset to the start of every record in a file. Since offsets are four bytes long, an array of dwords will work quite nicely2. Generally, as you construct the file you fill in the list (array) of offsets and keep track of the number of records in the file. For example, if you were creating a text file and you wanted to be able to quickly locate any line in the file, you would save the offset into the file of each line you wrote to the file. The following code fragment shows how you could do this:

...
Was This Post Helpful? 3
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Write to the middle of a file

Posted 08 June 2012 - 01:01 PM

You could also reference seekg() and other functions like it to get to the middle of the file...seekg(), not sure if this will help you too much, but it could lead you in the right direction.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Write to the middle of a file

Posted 08 June 2012 - 01:37 PM

View PostDavid W, on 05 June 2012 - 11:51 AM, said:

http://webster.cs.uc...TML/Files3.html

You might like to also see the article at section ...

7.5 ISAM (Indexed Sequential Access Method) Files

:

ISAM is actually a very simple concept. Somewhere, the program saves the offset to the start of every record in a file. Since offsets are four bytes long, an array of dwords will work quite nicely2. Generally, as you construct the file you fill in the list (array) of offsets and keep track of the number of records in the file. For example, if you were creating a text file and you wanted to be able to quickly locate any line in the file, you would save the offset into the file of each line you wrote to the file. The following code fragment shows how you could do this:

...


So the question though is "Does the OP need the records in the file to be physically arrange alphabetically, or is it sufficient that records presented to the user are alphabetical?"

If it is the former, then all the ISAM will do is help the OP figure where to do the insertion. He'll still have to figure out how to write in the middle of the file by moving the adjacent records to make room for the new record. Deleting a record will be a slightly easier exercise in writing to a middle of a file, and then truncating the file.

If it's the latter, then I completely agree that an ISAM would be an excellent approach for the OP with the caveat that all he ever does is add records. New users and passwords get appended to the file, and the indices are sorted so that the records they point to are in alphabetical order. When it is time to delete users, the index to that user can simply be deleted, but now we'll have unused space in the middle of the file. If the OP wants to recycle that space, he's back again to the original problem of how to write in the middle of file. Fortunately, if he picks the right space, it's only an issue of seeking to the unused space and just writing out the data without any fear of overwriting the adjacent records. There will still be fragments of unused space leftover though unless replacement records fit the unused space exactly. Modifying a record will also involve writing in the middle of file if the new record is smaller, or if there is unused space after the current record and the new record is bigger. If the new record is bigger, then a different unused space needs to be found, with appending to the end of the file as the last resort. If the OP wants to recover those fragments of unused space, he'll either have to do compaction by rewriting to a brand new file, or do an inplace compaction. The inplace compaction brings the problem of how to write in the middle of a file once more.

I like ISAMs. I've used them a lot when I had to do my own adhoc databases, or to speed up searches and scans. I just feel that there has to be full disclosure that ISAM won't completely bypass the need to write in the middle of file if there are delete or modify operations.
Was This Post Helpful? 2
  • +
  • -

#7 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Write to the middle of a file

Posted 08 June 2012 - 03:46 PM

Thanks guys! You all are giving me things to think about I never even considered before. Why I need the information sorted alphabetically is purely for search efficiency. Nobody should have access to the file itself so it doesn't need to be physically sorted. I liked the idea of just appending at the end of the file and somehow keeping it sorted behind the scenes. Unfortunately most of this is above my head so id need some clarification
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3589
  • View blog
  • Posts: 11,159
  • Joined: 05-May 12

Re: Write to the middle of a file

Posted 08 June 2012 - 11:52 PM

The best graphic I saw that demonstrated an ISAM was in an old Data Structures book that I had. I've not seen that book in ages, otherwise I would have been willing to bend a few copyright laws to scan and post it. It was a masterpiece of informative graphic art and technical documentation.

Sent from my T-Mobile G2 using Tapatalk 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1