Home AMX User Forum AMX General Discussion

Variable_To_String Limitations

Hello,

i had already use the Variable_To_String function to save data on flash drive.( with the file_write keyword)

Today i tried to save a structure, i already did that but never with interger array on the struct. it seems not working ! Someone tried it already?

my code example :
// Writing the struct on file
define_call 'Ecriture_Fichier_Preset_Grille_HDSDI'
{
    local_var
	slong File
	slong Convert_Success
	slong File_Result
	char Binary_String[90000]
    
    File = File_Open('/Grilles/Grille_HDSDI.txt',2)
    Convert_Success = Variable_To_String(Tab_Preset_Grille_HDSDI, Binary_String, 1)
    File_Result = File_Write(File, Binary_String, 90000)
    File_Close(File)
   
   
}
// reading struct from file
define_call 'Lecture_Fichier_Preset_Grille_HDSDI'
{
    local_var
	slong File
	slong Convert_Success
	slong File_Result
	char Binary_String[90000]
    
    File = File_Open('/Grilles/Grille_HDSDI.txt',1)
    Convert_Success = File_Read(File, Binary_String, 90000)
    File_Result = String_To_Variable(Tab_Preset_Grille_HDSDI, Binary_String, 1)
    File_Close(File)
   
}


for testing with the struct :
structure Struct_Preset_Grille_HDSDI
{
   Char Label[11][50]
   integer Preset_HDSDI[64]  // <= here is the issue !!!! 
   integer I_count // <= no issue
}

there is no error during encoding and decoding variable, no error during writing to file ans reading, but i never get back my integer array. Single integer is right read, but not array.
Char and char array are right read.


any ideas?

Comments

  • MathieuMathieu Posts: 25
    an idea

    I tried to change my way of thinking, so if we could only use char array, i convert all my interger array to char array and save my char array. after that we i read the char array i convert the string in integer... it's not a nice solution but it works..

    my decoding and encoding calls
    // Conversion de la structure pour bug fonction
    define_call 'Encoding_VGA_Struct'(Struct_Preset_Grille_VGA Tab_Preset_Grille_VGA[],Struct_Preset_Grille_VGA_Converted Tab_Preset_Grille_VGA_Encoded[], integer Struct_Size)
    {
        local_var
    	integer N_Count
    	integer M_Count
    	char String_Temp[3]
        
        for(N_Count=1;N_Count<=Struct_Size;N_Count++)
        {
    	for(M_Count=1;M_Count<=Groupe_Surveillance;M_Count++)
    	{
    	    Tab_Preset_Grille_VGA_Encoded[N_Count].Label[M_Count] = Tab_Preset_Grille_VGA[N_Count].Label[M_Count]
    	}
    	for(M_Count=1;M_Count<=8;M_Count++)
    	{
    	    String_Temp = itoa(Tab_Preset_Grille_VGA[N_Count].preset_VGA[M_Count])
    	    Tab_Preset_Grille_VGA_Encoded[N_Count].preset_VGA[M_Count] = String_Temp
    	}
        }
    } 
    
    // Conversion de la structure pour bug fonction
    define_call 'Decoding_VGA_Struct'(Struct_Preset_Grille_VGA Tab_Preset_Grille_VGA[],Struct_Preset_Grille_VGA_Converted Tab_Preset_Grille_VGA_To_Decode[], integer Struct_Size)
    {
        local_var
    	integer N_Count
    	integer M_Count
    	char String_Temp[3]
        
        for(N_Count=1;N_Count<=Struct_Size;N_Count++)
        {
    	for(M_Count=1;M_Count<=Groupe_Surveillance;M_Count++)
    	{
    	    Tab_Preset_Grille_VGA[N_Count].Label[M_Count] = Tab_Preset_Grille_VGA_To_Decode[N_Count].Label[M_Count]
    	}
    	for(M_Count=1;M_Count<=8;M_Count++)
    	{
    	    String_Temp = Tab_Preset_Grille_VGA_To_Decode[N_Count].preset_VGA[M_Count]
    	    Tab_Preset_Grille_VGA[N_Count].preset_VGA[M_Count] = atoi(String_Temp)
    	}
        }
    } 
    


    my final writing and reading functions
       
    // sauvegarde dans un fichier txt en binary mode
    define_call 'Ecriture_Fichier_Preset_Grille_VGA'
    {
        local_var
    	slong File
    	slong Convert_Success
    	slong File_Result
    	char Binary_String[90000]
    	//Struct_Preset_Grille_VGA_Converted Tab_Preset_Grille_VGA_Encoded[Taille_Tab_Preset_Grille]
        
        call 'Encoding_VGA_Struct'(Tab_Preset_Grille_VGA, Tab_Preset_Grille_VGA_Encoded, Taille_Tab_Preset_Grille)
       
        File = File_Open('/Grilles/Grille_VGA.txt',2)
        Convert_Success = Variable_To_String(Tab_Preset_Grille_VGA_Encoded, Binary_String, 1)
        File_Result = File_Write(File, Binary_String, 50000)
        File_Close(File)
        
        FILE_COPY ("'/Grilles/Grille_VGA.txt'","'/Grilles/Grille_VGA.bak'")
        
        
        //info to user
        send_string 0,"'AMX -> File : Save Presets VGA To File'"
    }
      
    // relecture du fichier cr?? avec la fonction "Save_Tab_Unit" d'un tableau Tab_Unit en binary
    define_call 'Lecture_Fichier_Preset_Grille_VGA'
    {
        local_var
    	slong File
    	slong Convert_Success
    	slong File_Result
    	char Binary_String[90000]
    	//Struct_Preset_Grille_VGA_Converted Tab_Preset_Grille_VGA_To_Decode[Taille_Tab_Preset_Grille]
        
       
        File = File_Open('/Grilles/Grille_VGA.txt',1)
        send_string 0,"'File : ',itoa(File)"
        Convert_Success = File_Read(File, Binary_String, 50000)
        send_string 0,"'Convert_Success : ',itoa(Convert_Success)"
        File_Result = String_To_Variable(Tab_Preset_Grille_VGA_To_Decode, Binary_String, 1)
        send_string 0,"'File_Result : ',itoa(File_Result)"
        File_Close(File)
    
        call 'Decoding_VGA_Struct'(Tab_Preset_Grille_VGA, Tab_Preset_Grille_VGA_To_Decode, Taille_Tab_Preset_Grille)
       
        //info to user
        send_string 0,"'File -> AMX : Load Presets VGA From File'"
    }
    
    
    my new temprary struc for the conversion
    // Presets de grille
    structure Struct_Preset_Grille_VGA
    {
       Char Label[Groupe_Surveillance][50] // 11 noms dans le preset,
       integer Preset_VGA[8] // indice = Input, valeur = Output
    }
    // convertion structure
    structure Struct_Preset_Grille_VGA_Converted
    {
       Char Label[Groupe_Surveillance][50] // 11 noms dans le preset,
       char Preset_VGA[8][3]
    }
    
  • AuserAuser Posts: 506
    Long story short, try
    set_length_array(Tab_Preset_Grille_HDSDI.Preset_HDSDI,max_length_array(Tab_Preset_Grille_HDSDI.Preset_HDSDI))
    

    BEFORE the struct is written out to file.

    Edit: Oops, typo in my code. Based on your code sample, the code would be as above.
  • MathieuMathieu Posts: 25
    why ?

    why does it work with char array and not with interger ? i don't really understand your answer...
Sign In or Register to comment.