accessing csv file

R

RobcPettit

Hi, I have software that records betfair data into a csv file. I have
no access to the code, al I can adjust is the intervals at which it
records, 1sec up, and which folder/file to save the data to. Ive
written some code myself which uses streemreader to read the file into
an array, which I then reverse as I only want the last 10-15 lines
entered in the file. I fire this code using a timer. My problem is
this: If the betfair software is set to update every 2 sec, and I set
my timer to fire every 3 secs, then at some point there will be a
clash accessing the file. This is infact what happens, sometimes it
takes 10minutes upon running, or straight away. Now it knocks the
betfair software out, but not mine because ineffect it just keeps
repeating the last line.
Is there away to tell my code to wait until file is free, asumming
betfair software is opening file, updating and closing, before
reading. Im thinking that when data's bieng written to the cv file,
its flaged to say its in use. I hope this makes sense.
Regards Robert
 
M

Marc Gravell

How would the other software react if the file was missing? Would it
simply recreate? Basically, even reading the file is probably going to
cause the other software to have issues when it tries to open the file
for write. However, renaming the file (move) is a single atomic
operation that should succeed if the file is currently closed, or
throw an error otherwise (just wait for retry)... either way the other
software should be happier to carry on (or at least, collisions should
be rarer). Of course, you'd still need to handle merging these
contents with what you have already seen...

Also - reading everything into an array and reverse? Once you have the
data, you don't really need to reverse it to get the last entries -
just look at the indexes and ignore everything else. Better still -
when reading the data just keep a buffer of 15 rows. As you read data,
simply round-robin the buffer (and keep track of the cursor) and you
should have a very efficient store of 15 rows.

Alternatively, you could use FileSystemWatcher to observe the files
being edited, but you'd need to be sure to read it (and close it)
before the competing process tries to write to it again - otherwise
you will likely kill the other software.

For reading CSV, there is a good reader on CodeProject; it handles a
lot of the subtle format issues with CSV:
http://www.codeproject.com/KB/database/CsvReader.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top