Login Page - Create Account

Support Board


Date/Time: Sun, 24 Nov 2024 18:30:22 +0000



Post From: Working example code to read a cvs file.

[2024-01-09 02:06:37]
User719512 - Posts: 263
Are you more concerned with Sierra providing an example, which perhaps they should, or solving your problem?
ChatGPT appears quite capable of answering this question.
The first answer it gave me seemed to be using older header as the function prototypes did not match current headers.
Once I told it the function prototypes, the code seemed pretty reasonable to me.
NOTE: it will not work 100% as-is, and you'll need a little boilerplate for all the usual Sierra study variables, sc.Defaults, subgraph declarations/etc.
It does however, show how to call sc.ReadFile.



I am helping someone who has questions about Sierra chart ReadFile API and how to use it. I don't think they have tried to ask ChatGPT themselves how to use the ReadFile API, and I thought I would see your response and forward that on to them.

Can you provide a minimal example of a Sierra chart study that uses sc.ReadFile API? For brevity in the code, let's assume the file is "c:\ninja2sierra.txt" and that file will contain a single line with a float that we will need to parse into a variable like "float data;".

...bad ChatGPT code omitted for brevity...


You must have old headers. The prototype for Sierra file operations are:
  int32_t (SCDLLCALL* OpenFile)(const SCString& PathAndFileName, const int Mode, int& FileHandle);
  int32_t(SCDLLCALL* ReadFile)(const int FileHandle, char* Buffer, const int BytesToRead, unsigned int* p_BytesRead);
  int32_t(SCDLLCALL* WriteFile)(const int FileHandle, const char* Buffer, const int BytesToWrite, unsigned int* p_BytesWritten);
  int32_t(SCDLLCALL* CloseFile)(const int FileHandle);

ChatGPT

I apologize for the confusion. It looks like there has been a change in the Sierra Chart API since my last update. Below is the updated example using the correct prototypes for file operations:


#include "sierrachart.h"

SCDLLNAME_API int32_t SCDLLCALL OpenFile(const SCString& PathAndFileName, const int Mode, int& FileHandle);
SCDLLNAME_API int32_t SCDLLCALL ReadFile(const int FileHandle, char* Buffer, const int BytesToRead, unsigned int* p_BytesRead);
SCDLLNAME_API int32_t SCDLLCALL WriteFile(const int FileHandle, const char* Buffer, const int BytesToWrite, unsigned int* p_BytesWritten);
SCDLLNAME_API int32_t SCDLLCALL CloseFile(const int FileHandle);

SCSFExport scsf_ReadFileExample(SCStudyInterfaceRef sc)
{
// Define the file path
const char* filePath = "c:\\ninja2sierra.txt";

// Declare variables
float data = 0.0;

// Open the file
int fileHandle;
if (sc.OpenFile(filePath, 0, fileHandle) == 1)
{
// Read the file contents
char buffer[256]; // Adjust the buffer size as needed
unsigned int bytesRead = 0;
if (sc.ReadFile(fileHandle, buffer, sizeof(buffer) - 1, &bytesRead) == 1)
{
buffer[bytesRead] = '\0'; // Null-terminate the buffer
// Parse the float value
if (sscanf_s(buffer, "%f", &data) == 1)
{
// Successfully read and parsed the float value
sc.AddMessageToLog("Read and parsed data from file.", 0);
sc.AddMessageToLog(sc.FormatStudySubgraphName(0), 0);
}
else
{
// Failed to parse the float value
sc.AddMessageToLog("Failed to parse data from file.", 1);
}
}
else
{
// Error reading file
sc.AddMessageToLog("Error reading file.", 1);
}

// Close the file
sc.CloseFile(fileHandle);
}
else
{
// File does not exist or could not be opened
sc.AddMessageToLog("File does not exist or could not be opened.", 1);
}

// Use the 'data' variable as needed

// Output the result to a subgraph for visualization
sc.Subgraph[0][sc.Index] = data;
}

ChatGPT

This script uses the correct file operation prototypes for Sierra Chart. Please adjust the buffer size and handle errors as needed for your specific use case.