Read TXT or CSV

 

Hi, I don't know why, but can't manage to read CSV or TXT

I have tried all the possible combinations for reading path, and always get the same results (error 5004 or 5002)

what do I have to change to read the txt?

2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   File does NOT exist!
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   File does NOT exist!
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   File check failed
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   Error: No se pudieron cargar las tasas de interés
2024.12.22 20:25:37.034 Core 01 tester stopped because OnInit returns non-zero code 1
2024.12.22 20:25:37.036 Core 01 disconnected


bool CheckFileDetails() {
    string filePath = "todas_las_tasas_2000_2024.txt";
    
    // Check if file exists
    if(!FileIsExist(filePath)) {
        Print("File does NOT exist!");
        return false;
    }
    
    // Try to get file size
    long fileSize = FileSize(filePath);
    Print("File exists. Size: ", fileSize, " bytes");
    
    // Try to open the file
    int handle = FileOpen(filePath, FILE_READ|FILE_TXT|FILE_ANSI);
    if(handle == INVALID_HANDLE) {
        Print("Cannot open file. Error code: ", GetLastError());
        return false;
    }
    
    // Read and print first few lines for debugging
    int lineCount = 0;
    while(!FileIsEnding(handle) && lineCount < 5) {
        string line = FileReadString(handle);
        Print("Line ", lineCount + 1, ": ", line);
        lineCount++;
    }
    
    FileClose(handle);
    return true;
}

bool LoadInterestRatesFromTxt() {
    string filePath = "C:\\Users\\jsgas\\Documents\\todas_las_tasas_2000_2024.txt";
    
    // First, check file details
    if(!CheckFileDetails()) {
        Print("File check failed");
        return false;
    }
    
    // Open file
    int handle = FileOpen(filePath, FILE_READ|FILE_TXT|FILE_ANSI);
    if(handle == INVALID_HANDLE) {
        Print("Error opening file. Error code: ", GetLastError());
        return false;
    }
    
    // Rest of your existing file reading code...
    // Skip header row
    FileReadString(handle);
    
    ArrayResize(g_interestRates, 1000);
    int idx = 0;
    
    while(!FileIsEnding(handle)) {
        string line = FileReadString(handle);
        if(line == "") break;
        
        // Split the line using semicolon
        string parts[];
        int count = StringSplit(line, ';', parts);
        
        if(count >= 5) {  // Ensure we have enough columns
            string dateStr = parts[0];
            string usdStr = parts[1];
            string jpyStr = parts[3];
            
            // Convert values
            datetime fecha = StringToTime(dateStr);
            double tasaUSD = StringToDouble(usdStr);
            double tasaJPY = StringToDouble(jpyStr);
            
            // Store in structure
            g_interestRates[idx].date = fecha;
            g_interestRates[idx].usdRate = tasaUSD;
            g_interestRates[idx].jpyRate = tasaJPY;
            g_interestRates[idx].interestDifferential = tasaUSD - tasaJPY;
            
            idx++;
            
            // Resize array if necessary
            if(idx >= ArraySize(g_interestRates)) {
                ArrayResize(g_interestRates, ArraySize(g_interestRates) + 1000);
            }
        }
    }
    
    // Adjust final array size
    ArrayResize(g_interestRates, idx);
    
    // Close the file
    FileClose(handle);
    
    Print("Interest rates loaded: ", idx, " entries");
    
    return idx > 0;
}
 
Javier Santiago Gaston De Iriarte Cabrera:

Hi, I don't know why, but can't manage to read CSV or TXT

I have tried all the possible combinations for reading path, and always get the same results (error 5004 or 5002)

what do I have to change to read the txt?

Show the log output while running the posted code.
 

This:

string filePath = "C:\\Users\\jsgas\\Documents\\todas_las_tasas_2000_2024.txt";

would try (and fails) an access outside of the MQ Sandbox!

 
Alain Verleyen #:
Show the log output while running the posted code.
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   File does NOT exist!
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   File does NOT exist!
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   File check failed
2024.12.22 20:25:37.034 Core 01 2000.01.01 00:00:00   Error: No se pudieron cargar las tasas de interés
2024.12.22 20:25:37.034 Core 01 tester stopped because OnInit returns non-zero code 1
2024.12.22 20:25:37.036 Core 01 disconnected

EDITED

 
Carl Schreiber #:

This:

would try (and fails) an access outside of the MQ Sandbox!

It doesn't reach there ... it says file doesn't exist ... (I tried all possibilities there (inside files)


 
Javier Santiago Gaston De Iriarte Cabrera #:

It doesn't reach there ... it says file doesn't exist ... (I tried all possibilities there (inside files)


In the Tester it will never exists unless you help the system :

#property tester_file "todas_las_tasas_2000_2024.txt"
 
Alain Verleyen #:

In the Tester it will never exists unless you help the system :

Great! Thanks!

bool LoadInterestRatesFromTxt()
{
   string filename = "todas_las_tasas_2000_2024.txt";
   
   // Print detailed file location information
   string common_folder = TerminalInfoString(TERMINAL_COMMONDATA_PATH);
   string full_path = common_folder + "\\" + filename;
   
   PrintFormat("Attempting to load file:");
   PrintFormat("Common Folder: %s", common_folder);
   PrintFormat("Full File Path: %s", full_path);
   
   // Check if file exists before attempting to open
   bool fileExists = FileIsExist(filename, FILE_COMMON);
   PrintFormat("File Exists in Common Folder: %s", fileExists ? "Yes" : "No");
   
   // Try different file opening modes
   int handle = FileOpen(filename, FILE_READ|FILE_TXT|FILE_COMMON);
   
   if(handle == INVALID_HANDLE) {
      int error = GetLastError();
      PrintFormat("Error opening file. Error code: %d", error);
      return false;
   }
   
   // Read the entire file content
   string file_content = "";
   while(!FileIsEnding(handle)) {
      file_content += FileReadString(handle) + "\n";
   }
   FileClose(handle);
   
   // Split the content into lines
   string lines[];
   int lines_count = StringSplit(file_content, '\n', lines);
   
   PrintFormat("Total lines in file: %d", lines_count);
   
   // Prepare array for interest rates
   ArrayResize(g_interestRates, 1000);
   int idx = 0;
   
   // Start from 1 to skip header
   for(int i = 1; i < lines_count; i++) {
      // Skip empty lines
      if(lines[i] == "" || lines[i] == "\r") continue;
      
      // Split the line using tab
      string parts[];
      int parts_count = StringSplit(lines[i], '\t', parts);
      
      // Ensure we have enough parts
      if(parts_count < 5) {
         PrintFormat("Skipping line %d: insufficient parts. Line: %s", i, lines[i]);
         continue;
      }
      
      // Remove leading/trailing whitespace manually
      string dateStr = StringTrimLeft(parts[0]);
      string usdStr = StringTrimLeft(parts[1]);
      string jpyStr = StringTrimLeft(parts[3]);
      
      // Remove trailing spaces
      dateStr = StringSubstr(dateStr, 0, StringLen(dateStr));
      usdStr = StringSubstr(usdStr, 0, StringLen(usdStr));
      jpyStr = StringSubstr(jpyStr, 0, StringLen(jpyStr));
      
      // Skip lines with empty values
      if(dateStr == "" || usdStr == "" || jpyStr == "") {
         PrintFormat("Skipping line %d: empty critical values", i);
         continue;
      }
      
      datetime fecha = StringToTime(dateStr);
      double tasaUSD = StringToDouble(usdStr);
      double tasaJPY = StringToDouble(jpyStr);
      
      // Store in structure
      g_interestRates[idx].date = fecha;
      g_interestRates[idx].usdRate = tasaUSD;
      g_interestRates[idx].jpyRate = tasaJPY;
      g_interestRates[idx].interestDifferential = tasaUSD - tasaJPY;
      
      idx++;
      
      // Resize array if necessary
      if(idx >= ArraySize(g_interestRates)) {
         ArrayResize(g_interestRates, ArraySize(g_interestRates) + 1000);
      }
      
      // Limit to prevent potential infinite loop
      if(idx >= 300) break;
   }
   
   // Adjust final array size
   ArrayResize(g_interestRates, idx);
   
   PrintFormat("File reading complete:");
   PrintFormat("Total lines processed: %d", idx);
   PrintFormat("Interest rates loaded: %d entries", idx);
   
   // Log first and last entries for verification
   if(idx > 0) {
      PrintFormat("First entry - Date: %s, USD Rate: %.2f, JPY Rate: %.2f", 
                  TimeToString(g_interestRates[0].date, TIME_DATE), 
                  g_interestRates[0].usdRate, 
                  g_interestRates[0].jpyRate);
      
      PrintFormat("Last entry - Date: %s, USD Rate: %.2f, JPY Rate: %.2f", 
                  TimeToString(g_interestRates[idx-1].date, TIME_DATE), 
                  g_interestRates[idx-1].usdRate, 
                  g_interestRates[idx-1].jpyRate);
   }
   
   return idx > 0;
}
#property tester_file "todas_las_tasas_2000_2024.txt"
   ArrayResize(g_interestRates, 0);

// Intentar cargar las tasas de interés
   if(!LoadInterestRatesFromTxt())
     {
      Print("Error: No se pudieron cargar las tasas de interés");
      return(INIT_FAILED);
     }