Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File Class

Posted on 2003-11-17
6
Medium Priority
?
311 Views
Last Modified: 2010-04-02
Hello,

 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.


Ramani
0
Comment
Question by:ramani081399
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:KurtVon
ID: 9764119
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.
0
 

Author Comment

by:ramani081399
ID: 9764172
Kurt,
 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.

Thanks

Ramani
0
 
LVL 11

Expert Comment

by:KurtVon
ID: 9764386
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++: http://www.zdnet.com.au/builder/program/java/story/0,2000034779,20279107,00.htm

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

Hope this helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ramani081399
ID: 9764885
Kurt,
 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.
Ramani
0
 
LVL 11

Accepted Solution

by:
KurtVon earned 750 total points
ID: 9765338
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.

0
 
LVL 5

Expert Comment

by:BlackDiamond
ID: 9767555
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question