Login Page - Create Account

Support Board


Date/Time: Thu, 28 Nov 2024 00:23:20 +0000



Post From: Read & parse CSV

[2020-01-06 13:14:36]
User462086 - Posts: 196
Hi, in order to read a CSV file from disk then parse out certain fields from each line I've been trying to get 'sc.ReadFile' to work but can't seem to figure it out (code below with comments compiles fine). Stack Overflow has tons of examples for C++ but most utilize 3rd party libraries. In the past I've used 'fgets' in AmiBroker's C-based scripting language.

1) What's the best way to accomplish this in Sierra Chart?
2) If 'sc.ReadFile' is the best way, how do I get the file's text from the 'Buffer' (pointer)

Many thanks!!

Edit: using version 2028



#include "sierrachart.h"

SCDLLName("Test_ReadFile")

// support function

DWORD GetFileSize(const SCString& PathAndFileName){
  
  HANDLE File = CreateFile
          ( PathAndFileName
          , GENERIC_READ
          , FILE_SHARE_READ | FILE_SHARE_WRITE
          , NULL
          , OPEN_EXISTING
          , 0
          , NULL
          );
  
  if (File == INVALID_HANDLE_VALUE)
    return INVALID_FILE_SIZE;
  
  DWORD FileSize = ::GetFileSize(File, NULL);
  
  CloseHandle(File);
  
  return FileSize;
}

// main

SCSFExport scsf_Test_ReadFile(SCStudyInterfaceRef sc){

  SCString DebugMessage;
    // int/bool %d; float %f, %g, %e; string %s
    // DebugMessage.Format("caption : %d",var);
    // sc.AddMessageToLog(DebugMessage, 0);
  
  SCInputRef FullPath  = sc.Input[0];
  
  if (sc.SetDefaults){
    sc.GraphName = "Test_ReadFile";
    sc.AutoLoop = 0;
    FullPath.Name = "Full Path to Text File";
    FullPath.SetPathAndFileName("");
    return;
  }

  int FileHandle;

  bool FileOpened = sc.OpenFile
            ( FullPath.GetPathAndFileName()
            , n_ACSIL::FILE_MODE_OPEN_EXISTING_FOR_SEQUENTIAL_READING
            , FileHandle
            );
    
  if (FileOpened==false)
    return;
  
  DWORD BytesToRead = GetFileSize(FullPath.GetPathAndFileName());
  
  if (BytesToRead==INVALID_FILE_SIZE)
    return;
  
DebugMessage.Format( "BytesToRead : %d", BytesToRead );
sc.AddMessageToLog( DebugMessage, 0 );  
  
  char* Buffer;
  unsigned int* p_BytesRead;
  
  bool FileRead = sc.ReadFile
            ( FileHandle
            , Buffer
            , BytesToRead
            , p_BytesRead
            );
  
  if (FileRead==false)
    return;        // <- this executes = something is wrong with my 'sc.ReadFile' call
  
DebugMessage.Format( "Buffer : %s", Buffer ); // <- when the line above is removed this returns "(null)"
sc.AddMessageToLog( DebugMessage, 0 );
  
  sc.CloseFile(FileHandle);
}



Date Time Of Last Edit: 2020-01-06 13:20:20