[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Re: fopen



PureBytes Links

Trading Reference Links

Ed,
 
I beleive for a text file, only way to handle it is to read the file (line by line), make the chages you want and create a second file with the modifications.The delete original file.
----- Original Message -----
Sent: Sunday, July 22, 2007 7:59 AM
Subject: Re: [amibroker] Re: fopen

hi GP,
 
yes I thought of that as well  and this is easy to implement however since it seems possible to use
 
// open file
fh =
fopen(basketName, "r+");
 
I was hoping someone would know how. My AFL routine compares the real number of shares in my portfolio with the number of shares in my basket file. I want to adjust the numbers in the basketfile which is usually a small correction.
 
Would you know how to do such a thing in AFL? Does AFL also work with pointers?
 
thanks, Ed
 
 
 
 
----- Original Message -----
From: gp_sydney
Sent: Sunday, July 22, 2007 4:43 PM
Subject: [amibroker] Re: fopen

Ed,

Using fseek etc. to randomly access a file is only useful if the data
size stays exactly the same. For example, if you wanted to replace 247
with 2947 you'd have a problem because the replacement is longer,
which means the rest of the file would need to all be moved down a
character.

The more common way to handle this is to effectively copy the file to
a new one and replace the values you want as you go. That way the two
files can end up different lengths.

The way I'd typically do it would be:

- Open the existing file with read access.
- Create a new temporary file with write access.
- Read from the existing and write to the new with your modifications.
- Close both files.

If all that goes smoothly, then:

- Delete the original file.
- Rename the temporary file to the name of the original one.

For temporary files, I usually pick something like $$MyTemp.$$$ as a
filename to minimise the risk that the file might exist already with
something else in it, otherwise it would get deleted. If you're really
concerned about that, you can test for it by trying to open the file
first with read-only access. If the open fails, then you know the file
doesn't exist. If the open is successful though, then it does (and
should be closed again). Of course it could just be an unwanted
temporary file from a previous pass that never got deleted for some
reason, which is why I generally just pick a highly unlikely name and
don't even bother testing if it exists already.

Regards,
GP

--- In amibroker@xxxxxxxxxps.com, "Edward Pottasch" <empottasch@...>
wrote:
>
> hi,
>
> I want to replace numbers inside a file. For instance if I have a
text file containing the lines:
>
> SELL, 247, JNS, STK, SMART, LMT, 32.05, 3,
> SELL, 244, SIGM, STK, SMART, LMT, 34.15, 3,
>
> I want to replace 247 with 248 and 244 with 245.
>
> According to my latest information this should be possible in
Amibroker using fh = fopen(basketName, "r+");
>
> The C runtime library mode "r+"
>
> I have been looking for examples how to do such a think in C. It
seems that I will need additional functions like rewind, fseek etc
which do not seem available in AFL. I haven't got a clue but it
should be possible in AFL as well. Anyone with experience in C would
know how to do this?
>
> Below some code that reads a file containing the lines I want to
replace. Now I need additional code that does the actual replacement.
>
> thanks, Ed
>
>
>
>
> basketName = "C:\\tt.txt";
>
>
> // open file
> fh = fopen(basketName, "r+");
>
> if(fh) {
>
> while( !feof(fh )) {
>
> // format basket line: SELL, 100, CHRW, STK, SMART, LMT,
52.44, 1,
> ss = fgets(fh);
>
> // string length
> sl = StrLen(ss);
> // storage array
> strpos = 0;
>
> cnt = 0;
> for (i=0; i<sl; i++) {
>
> if (StrMid(ss,i,1) == ",") {
>
> strpos[ cnt ] = i;
> cnt = cnt + 1;
>
> }
>
> }
>
> // check if we did read a blank line
> if (cnt > 0) {
>
> // extract action (BUY or SELL)
> sp1 = strpos[ 0 ];
> actn = StrMid(ss,0,sp1);
>
> // extract number of shares
> sp2 = strpos[ 1 ];
> nos = StrMid(ss,sp1 + 2, sp2 - sp1 - 2); nos = StrToNum(nos);
>
> // extract ticker, e.g. CHRW
> sp3 = strpos[ 2 ];
> tckr = StrMid(ss,sp2 + 2,sp3 - sp2 - 2);
>
> // extract limit price
> sp6 = strpos[ 5 ];
> sp7 = strpos[ 6 ];
> lprc = StrMid(ss,sp6 + 2,sp7 - sp6 - 2); lprc =
StrToNum(lprc);
>
> // extract whether trade is (1) an entry order, (2) an exit
order, or (3) a profit stop order
> sp7 = strpos[ 6 ];
> sp8 = strpos[ 7 ];
> ee = StrMid(ss,sp7 + 2,sp8 - sp7 - 2); ee = StrToNum(ee);
>
>
>
> // construct new line
> newl = actn + ", " + nos + ", " + tckr + ", " + "STK,
SMART, LMT, " + ee + ",";
>
> }
>
> }
>
> fclose(fh);
>
> } else {
>
> printf("ERROR: file can not be found (does not exist)");
>
> }
>

__._,_.___

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html





SPONSORED LINKS
Investment management software Investment property software Investment software
Investment tracking software Return on investment software

Your email settings: Individual Email|Traditional
Change settings via the Web (Yahoo! ID required)
Change settings via email: Switch delivery to Daily Digest | Switch to Fully Featured
Visit Your Group | Yahoo! Groups Terms of Use | Unsubscribe

__,_._,___