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)");
>
>
}
>