Reading array from file.

jsth17
2020-11-11
2020-11-11
  • jsth17 - 2020-11-11

    I have been trying to read an array of numbers in a "txt" file from a SD-card with the Sysfile library. The program seems to recognize the file and read some data. The problem is that the number of datapoints is off, and the values make no sense compared with the input file (2.52E-9 instead of the input 100.1).
    The code i use is seen below.

    PROGRAM Trajectory_read
    VAR
        i:UINT;
        done:BOOL;
        ref_xp_buffer: ARRAY [0..20000] OF REAL;
        filename_trajectory:STRING:='trajectory2.txt';
        file_handle : sysFile.RTS_IEC_HANDLE;
        result_trajectory : POINTER TO sysFile.RTS_IEC_RESULT; 
        Read_function: __XWORD;
        Close_function: UDINT;
        nohandle : BOOL := FALSE;
        test: POINTER TO REAL := ADR(ref_xp_buffer);
    
    END_VAR
    
    file_handle := sysFile.SysFileOpen(szFile:=filename_trajectory , am:=AM_READ , pResult:=result_trajectory);
    Read_function:=sysFile.SysFileRead(hFile:=file_handle , pbyBuffer:=test , ulSize:=10001 , pResult:=result_trajectory);
    Close_function:=sysFile.SysFileClose(hFile:=file_handle);
    

    The file contains an array of 10000 datapoint, and i have tried seperating the the datapoint by "," and "." and " ". I have also tried making it all in one column instead of a row. And i still can't get it to work

    Thanks in advance

     
  • JAPIB

    JAPIB - 2020-11-11

    Hi,

    J think you can't read file in only one time.
    You have to read number by number, until you reach the end of file.
    Depending the way you used to write your file :
    1Β°) If each number as the same format you can define what you want to read by the length of a number
    2Β°) if you used a separation character, you can read character by character and concat characters into a string, until you reach the end of line or this special separating character.
    3Β°) if you write your file with columns (Excel) you can read cell by cell, until you reach the end of the column or the end of line. And perhaps you need read character by character as in the second solution.

    In all cases, don't forget to convert the string you read into numerical value.

    BR

     

Log in to post a comment.