File Class


 I would like to write a class using fstream such that i can write to the file at the current position as well as read from the file at a different position. The read and write operations requests may happen at the same time.
To, explain this further, there could be one process that is writing to this file while another process may be reading from the same file at the same time. When it is reading it should read and write to the database. The file is a plain txt file containing database field values which are Strings,Integers,Longs etc.

I would appreciate a solution explaining as to how to do this. If you can post the code i would be thankful.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bleh, messy problem.

Probably the easiest way to do this is to open the file for read/write access, and use the SeekPos function to get to the proper place (I suppose you are going to need to track the current read and write positions).  To keep the reads and write from colliding, you will need to use thread locking to eliminate conflicts.

So a read would have to do something like this:

  create a lock on the stream
  move to the read position
  read the data
  record the new read position
  release the lock on the stream

and to write you would

  create a lock on the stream
  move to the write position
  write the data
  record the new write position
  releas ethe lock on the stream

Hope this helps.
ramani081399Author Commented:
 Could you post a piece of the code how this is done so that both read and write take place at the same time. That is reading will be at one position and writing from a different position of the file to the database.


The point is reading and writing can't be done at the same time.  This is a situation where you need to use thread locking to prevent that from happening.  Unfortunately, it can get a bit difficult being more specific without knowing what constraints you have on the program.

Here's an article on the subject using STL, which should be included with most versions of C++:,2000034779,20279107,00.htm

It's alot easier if you don't need to make it platform-portable, though.

Hope this helps.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

ramani081399Author Commented:
 I am constrained by time and am looking for a code to implement this. The article does explain use of STL's but i am looking for how to do this.
As I said, it's a messy problem.  It's unlikely that you will find any code better than the listings in the article (at least, general purpose-wise).  It might be better to think of a way around the problem instead.

For example, would it be possible to keep the file in memory, and make any "read" come from the copy there.  A write can change the memory copy and then write the needed data out to disk.  This would not only make the data inherently thread safe (you only need to block re-entry on the writes), but it would also speed reads up enormously.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Or better yet, don't read or write to a file. Instead, have the applications connect to a database and let it do this work for you.  Most good databases have had many developers spend countless hours on the most efficient cache and write algorithms.  Best advice is to take advantage of their work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.