Home AMX User Forum NetLinx Studio

Writing an Integer Array to Flash

Greetings,

I know that this is incomplete code, but hopefully you'll get the point!
INTEGER nIntegerArray[10]

VARIABLE_TO_STRING (nIntegerArray,cConvertedValue,1)
FILE_WRITE (slFileHandle,cConvertedValue,LENGTH_STRING(cConvertedValue))

Is the second line of code above correct for encoding the entire array? Or must there be a [] after nInetgerArray?

Thanks.

Comments

  • DHawthorneDHawthorne Posts: 4,584
    No, the brackets aren't needed unless you are declaring the array or specifying a particular element. If you want the whole thing, that is the correct syntax ... just make sure the target string is big enough to hold it along with the overhead to store the array structure.
  • Thanks

    Another problem is how do I get all of the integer value from the encoded string back into the integer array in one shot?
    STRING_TO_VARIABLE (nValue,cConvertedValue,1)  //In a function
    RETURN nValue
    
    nIntegerArray[]=ReadIntegerFromFlash('FILE_NAME.dat')   //In a main program event
    
  • viningvining Posts: 4,368
    Any time I'm creating code that writes to a file I use the error values returned by the FILE_OPEN & FILE_WRITE functions in a send_string 0 so I can see if errors are returned. It will save you from wasting alot of time.
    DEFINE_FUNCTION fnFileWrite_ToFile(CHAR iWriteFileName[],CHAR iWriteFileBuf[],INTEGER iIOFlag)
         {                                 //filename      //var w/ data to write // 1 (FILE_READ_ONLY) = The file is opened with READ ONLY status
         stack_var slong nWriteFHandle ;                                         // 2 (FILE_RW_NEW)    = The file is opened with READ WRITE status
         stack_var slong nWriteFResult ;                                        // 3 (FILE_RW_APPEND) = The file is opened with READ WRITE status
         
         nWriteFHandle = file_open(iWriteFileName,iIOFlag) ;
         if (nWriteFHandle > 0) 
    	  {
    	  if(nMain_DeBug)
    	       {
    	       SEND_STRING 0,"'FUNCTION fnFileWrite_ToFile. File_Open successful for *',
    					     iWriteFileName,'*! line-<',ITOA(__LINE__),'>',crlf" ;
    	       }
    	  nWriteFResult = FILE_WRITE (nWriteFHandle,iWriteFileBuf,length_string(iWriteFileBuf)) ;
    	  if(nWriteFResult > 0) 
    	       {
    	       if(nMain_DeBug)
    		    {
    		    SEND_STRING 0,"'FUNCTION fnFileWrite_ToFile. File_Write OK. Wrote a ',
    					itoa(nWriteFResult),' CHAR String!  line-<',ITOA(__LINE__),'>',crlf" ; 
    		    }
    	       }
    	  else
    	       {
    	       stack_var char cErrorMsg [20] ;
    	       
    	       switch (itoa(nWriteFResult)) 
    		    {
    		    case '-11': {cErrorMsg = 'disk full'} ;
    		    case '-6' : {cErrorMsg = 'invalid parameter buffer !> 0'} ;
    		    case '-5' : {cErrorMsg = 'disk I/O error'} ;
    		    case '-1' : {cErrorMsg = 'invalid file handle'} ;
    		    }
    	       if(nMain_DeBug)
    		    {
    		    SEND_STRING 0,"'FUNCTION fnFileWrite_ToFile. Bad File_Write_Line: ',cErrorMsg,
    						  '! line-<',ITOA(__LINE__),'>',crlf" ;
    		    }
    	       }
    	  }
         else
    	  {
    	  stack_var char cErrorMsg [40] ;
    	  
    	  switch (itoa(nWriteFHandle)) 
    	       {
    	       case '-2': {cErrorMsg = 'invalid file path or name'} ;
    	       case '-3': {cErrorMsg = 'invalid value supplied for IOFlag'} ;
    	       case '-5': {cErrorMsg = 'disk I/O error'} ;
    	       case '-14':{cErrorMsg = 'max number of files are already open'} ;
    	       case '-15':{cErrorMsg = 'invalid file format'} ;
    	       }
    	  if(nMain_DeBug)
    	       {
    	       SEND_STRING 0,"'FUNCTION fnFileWrite_ToFile. Bad File_Open: ',cErrorMsg,
    						  '! line-<',ITOA(__LINE__),'>',crlf" ;
    	       }
    	  }
         file_close(nWriteFHandle) ;
         
         RETURN ; 
         }  
    
  • AMXJeffAMXJeff Posts: 450
    Thanks
    Another problem is how do I get all of the integer value from the encoded string back into the integer array in one shot?
    DEFINE_FUNCTION WriteIntegerArrayFlash(CHAR cFileName[], INTEGER nValue[])
    {	
    	STACK_VAR SLONG slFileHandle;
    	STACK_VAR CHAR cConvertedValue[1000];
    	
    	// SOME BUG WITH VARIABLE TO STRING... INTEGER ARRAYS NEED LENGTH
    	IF (LENGTH_ARRAY(nValue) == 0)
    		SET_LENGTH_ARRAY(nValue,MAX_LENGTH_ARRAY(nValue));
    		
    	// NOT ENOUGH SPACE IN YOUR ARRAY	
    	IF (MAX_LENGTH_STRING(cConvertedValue) < LENGTH_VARIABLE_TO_STRING(nValue))
    	{
    		SEND_STRING 0,"'WriteIntegerArrayFlash: Error, NOT ENOUGH SPACE!!!'"
    		
    		RETURN;
    	}	
    
    	slFileHandle = FILE_OPEN(cFileName, FILE_RW_NEW);
    	
    	// VALID FILE HANDLE
    	IF (slFileHandle > 0)
    	{
    		VARIABLE_TO_STRING(nValue, cConvertedValue, 1);
    		
    		FILE_WRITE(slFileHandle, cConvertedValue, LENGTH_STRING(cConvertedValue));
    		
    		FILE_CLOSE(slFileHandle);
    	}
    }
    
    DEFINE_FUNCTION ReadIntegerArrayFlash(CHAR cFileName[], INTEGER nValue[])
    {	
    	STACK_VAR SLONG slFileHandle;
    	STACK_VAR CHAR cConvertedValue[1000];
    	
    	// NOT ENOUGH SPACE IN YOUR ARRAY	
    	IF (MAX_LENGTH_STRING(cConvertedValue) < LENGTH_VARIABLE_TO_STRING(nValue))
    	{
    		SEND_STRING 0,"'ReadIntegerArrayFlash: Error, NOT ENOUGH SPACE!'"
    		
    		RETURN;
    	}	
    
    	slFileHandle = FILE_OPEN(cFileName, FILE_READ_ONLY);
    	
    	// VALID FILE HANDLE
    	IF (slFileHandle > 0)
    	{		
    		FILE_READ(slFileHandle, cConvertedValue, MAX_LENGTH_STRING(cConvertedValue));
    		
    		STRING_TO_VARIABLE(nValue, cConvertedValue, 1);
    
    		FILE_CLOSE(slFileHandle);
    	}
    }
    
Sign In or Register to comment.