| Hello,     >I thought of StrReplace also. You can read a single line 
from a file, save it in a string array, replace a certain part in 
this string,  but then you will not be able to put it back to the same 
file.   At >least I do not know how to do it in AFL.   
 Indeed this can be used if you write it to another file using AFL 
only.   This part is easy. fgets - this function reads entire LINE into string variable. 
You can read all lines using simple while loop and feof()   fh = fopen("filename", "r" );   line = 0; while( ! feof( fh ) ) {   VarSetText("line"+(line++), fgets( fh )); }   fclose( fh );   // now you have all lines in lineNNN variables /// manipulate them here and then write to the SAME 
file   
fh = fopen("filename", "w" );   for( i = 0; i < line; i++ ) {  fputs( VarGetText("line"+i, fh ); }   fclose( fh ); Best regards,
 Tomasz 
Janeczko
 amibroker.com
 
  __._,_.___----- Original Message -----  Sent: Sunday, July 22, 2007 7:54 PM Subject: Re: [amibroker] Re: fopen 
 using "r+" works but it is not that simple. It 
  works as in C and I believe opening a file with "r+" is a pointer and in C you 
  need certain functions to work with them (fseek, rewind .... i believe).  
  As far as I understand now it can be done but not in AFL alone.   I thought of StrReplace also. You can read a 
  single line from a file, save it in a string array, replace a certain part in 
  this string,  but then you will not be able to put it back to the 
  same file.   At least I do not know how to do it in AFL.   
   Indeed this can be used if you write it to another file using AFL 
  only.   regards, Ed         
    ----- Original Message -----  Sent: Sunday, July 22, 2007 7:24 
    PM Subject: RE: [amibroker] Re: 
fopen 
 
    
     If the r+ thingy handles the rewrite of a different 
    sized file then you can do it without 2 files.   Also, you can simplify the whole thing by looking at 
    the string functions in AFL: StrReplace, etc.   d 
      
      
      hi Ara,   I asked for this functionality in the 
      feedback centre and get a reply that this functionality exists. Indeed 
      "r+" seems to be working. However I am not sure if it can be solved in AFL 
      entirely or that one needs to write a dll for it, or some script.  I 
      focus on AFL so I guess I will need to write the 
      adjusted file to a new filename ...    Also I guess it would not be so hard to 
      write a function in C++ that replaces some text in the form of a dll, nice 
      would be to solve it in AFL.   regards, ed         
        ----- Original Message -----  Sent: Sunday, July 22, 2007 7:00 
        PM Subject: Re: [amibroker] Re: 
        fopen 
 
        
         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 -----  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
   
 __,_._,___
 |