Click to See Complete Forum and Search --> : Parsing active text files... ?


yraen
April 10th, 2008, 02:22 PM
I'll preface this with the note that I am very new to C, but have been working with VB/VBS and other scripting languages for quite some time...

What I'm trying to do is create a program to read through an actively modified text file and return statistics. As an example, I want to open a text file that is constantly added to, but I only want to read the lines from the time I select to the file to the time I tell it to stop.

Example 1:

Text file with lines in the following format: [Mon Apr 08 14:27:37] SALE $14.73
I want a loop to constantly read the newly added lines for the word "SALE", pull the dollar amount and the time stamp out, added the dollars and create a $/Second ratio output.

Example 2:

I open the file at 2:26:59 with C, then collect lines from that starting point out to give me a real time number output

It would read the line, then update 3 (or 1) labels with the corresponding Time/Dollars/ratio after doing the math to get the numbers.

[Mon Apr 08 14:27:00] SALE $14.73
Time: 0
Dollars: 14.73
$/Second: 14.73
[Mon Apr 08 14:27:54] SALE $21.12
Time: 54
Dollars: 35.85
$/Second: 0.66
[Mon Apr 08 14:28:21] SALE $4.67
Time: 81
Dollars: 40.52
$/Second: 0.50
[Mon Apr 08 14:28:39] SALE $31.54
Time: 99
Dollars: 72.06
$/Second: 0.73
[Mon Apr 08 14:29:00] SALE $19.24
Time: 120
Dollars: 91.30
$/Second: 0.76
[Mon Apr 08 14:29:12] SALE $2.03
Time: 132
Dollars: 93.33
$/Second:
[Mon Apr 08 14:29:45] SALE $15.94
Time: 165
Dollars: 109.27
$/Second: 0.71
[Mon Apr 08 14:30:27] SALE $12.87
Time: 201
Dollars: 122.14
$/Second: 0.61
[Mon Apr 08 14:30:29] SALE $6.42
Time: 209
Dollars: 128.56
$/Second: 0.62
[Mon Apr 08 14:31:47] SALE $28.89
Time: 287
Dollars: 157.45
$/Second: 0.55
[Mon Apr 08 14:31:50] SALE $17.40
Time: 290
Dollars: 174.85
$/Second: 0.60
[Mon Apr 08 14:32:04] SALE $11.33
Time: 304
Dollars: 186.18
$/Second: 0.61
[Mon Apr 08 14:32:26] SALE $1.04
Time: 326
Dollars: 187.22
$/Second: 0.57

I don't necessarily want the code to do this, just some direction. I want this to utilize the fewest PC resources as possible (it will be running alongside other applications). My main questions here are how to loop the file read without maxing the CPU usage, how to read only the new lines, and the slightly smaller question of how to do the math on the times.

Any help is appreciated, thanks!

zdavis
April 10th, 2008, 02:35 PM
I would probably use a streamreader to read the lines. Are you going to be accessing this file while it is open. if so you will probably get an error about the file is being used by another process. if that is the case you can just make a copy of it everytime you want to check and then just access the copy and delete it after use. as far as only accessing the lines that are new. I would keep a counter of the total number of lines or a counter of the last line you read. everytime you want to start where you left off you can either just start at the next line past the counter or just loop through the number of lines up to the counter and start reading from there.

I hope that helps

yraen
April 10th, 2008, 02:39 PM
I would probably use a streamreader to read the lines. Are you going to be accessing this file while it is open. if so you will probably get an error about the file is being used by another process. if that is the case you can just make a copy of it everytime you want to check and then just access the copy and delete it after use. as far as only accessing the lines that are new. I would keep a counter of the total number of lines or a counter of the last line you read. everytime you want to start where you left off you can either just start at the next line past the counter or just loop through the number of lines up to the counter and start reading from there.

I hope that helps

Yeah, I'll be reading it while another application is appending lines to it. Sounds like possibility to work with, not sure how efficient the copy method may be as the text files can possibly be >100mb... not sure how well C# deals with files of that size, I know vbs would use a lot of CPU power to read em :o

zdavis
April 10th, 2008, 02:53 PM
is there any time in which the file won't be open

is it comma delimited?

yraen
April 10th, 2008, 02:57 PM
is there any time in which the file won't be open

is it comma delimited?

As far as I know, it would be open the entire time I was trying to access it (I only want to access it read-only). And it's not comma delimited. The text file itself would look something like:

[Mon Apr 08 14:27:00] SALE $14.73
[Mon Apr 08 14:27:54] SALE $21.12
[Mon Apr 08 14:28:21] SALE $4.67
[Mon Apr 08 14:28:39] SALE $31.54
[Mon Apr 08 14:29:00] SALE $19.24
[Mon Apr 08 14:29:12] SALE $2.03
[Mon Apr 08 14:29:45] SALE $15.94
[Mon Apr 08 14:30:27] SALE $12.87
[Mon Apr 08 14:30:29] SALE $6.42
[Mon Apr 08 14:31:47] SALE $28.89
[Mon Apr 08 14:31:50] SALE $17.40
[Mon Apr 08 14:32:04] SALE $11.33
[Mon Apr 08 14:32:26] SALE $1.04

I've done other things with these same text files, but it was always while they were not in use by another application, so I was just reading old saved data...

zdavis
April 10th, 2008, 03:27 PM
when you say it will be open the entire time. does that mean it is open 24/7 and never closes. does it ever get deleted?

yraen
April 10th, 2008, 03:39 PM
It's open for the entire duration that the other application is running, unless specifically closed by the user. Only gets deleted/moved/etc if the user does this manually. In order to do what I want, the file has to remain opened by the other application during this process so that it can add new lines for my app to process the stats for/with.

Going to play around with it a bit and see if I can come up with anything... thanks for the help so far! :)