Problems with WinAPI

 

Hi all, I have a strange result with _lread in winAPI:

 I set a string (char) as 50 x's, then I set another string (char1) as the value of char, then I use char1 with _lread.. somehow _lread sets both strings to the characters read from the file!

bool BidAskLineReader (string path, int handle)
   {
   string char, char1;
   char = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
   char1 = char;
   Alert("///STARTING char: ",char,"   char1: ",char1);
   int result=_lread(handle,char1,50); //read file
   Alert("///ENDING char: ",char,"   char1: ",char1);
   return (TRUE);
   }

gives this result:

 

 

Actually, this is not my main problem but it might be related.

This is my main problem:  I need to make the number of characters read from file to be variable, so when using char1 with _lread, I tried something like this:

int num = 6;
string char1=StringSubstr("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",0,num);
int result=_lread(handle,char1,num);

it gives:

result = 6    (good! it read 6 characters)

char1 = xxxxxx   (not good, it left them as x's)

I hope real programers will know what's going on here..? :/

Files:
 
alladir: somehow _lread sets both strings to the characters read from the file!
  1. Looks like File Operations via WinAPI - MQL4 Articles
  2. char1 = char;
    Apparently MT4 uses COW semantics for strings. That results in one buffer with two names.

  3. Don't call GetLastError, unless you have an error
       if(result<0){
            Alert("Error placing the pointer" );  // Capture GLE here.
            return (-1);
          }
        
       error = GetLastError();                   // No error (result positive)
       if(error!=0)                              // GLE may be ANYTHING, meaningless.
          {
          Alert("error during open before array init:",error);
          }
       error = GetLastError();                   // This will ALWAYS return zero.
       if(error!=0)
          {
          Alert("error during open:",error);
          }
        return(handle);
        }
    

 

Thanks, yes I stole most of the code from that article.

Any ideas on the second problem...? Why char1="xxxxxx" works, but char1=StringSubStr("xxxxxxxxxx",0,6) doesn't work.. I feel they should be identical so there's some things I'm just not understanding.

 

As for the GetLastErrors, I hoped that with with no error's, it's not called, so no problem. I make so many small silly mistakes that it's useful to help me understand and locate problems until the code is finished and I can delete them. Am I doing this the wrong way?

 

edit:

Well I still don't understand the problem but I'll set the number of x's with a switch.. should work