Login Page - Create Account

Support Board


Date/Time: Mon, 25 Nov 2024 05:15:07 +0000



Post From: How to read file with sc.ReadFile()?

[2022-05-21 01:00:56]
ertrader - Posts: 672
Thanks for posting this, it has helped me. I would like to read in a single value from a text file, then draw a line with that value. To draw the line, the value retrieved from the text file has to be converted from text to float so there is code added for this on the last line.

The code below works fine, it's just that I would like to get the performance in the sub 5 ms. It's currently about 30-40 ms on a ramdisk. Is there a better/faster way to accomplish what I'm trying to do?

#include "sierrachart.h"
#include <stdio.h>
#include <stdlib.h>

SCString ReadTextFile(SCStudyInterfaceRef sc, SCString FileLocation)
{
char TextBuffer[1000] = {}; // string size 1k, adjust as needed,
int FileHandle = 1; // File Handle cannot be just an integer numbers, has to be defined first, sc.OpenFile requires int&
unsigned int *p_BytesRead = new unsigned int(0); // sc.ReadFile() requires unsigned int pointer
sc.OpenFile(FileLocation.GetChars(), n_ACSIL::FILE_MODE_OPEN_EXISTING_FOR_SEQUENTIAL_READING, FileHandle);
sc.ReadFile(FileHandle, TextBuffer, 1000, p_BytesRead);
sc.CloseFile(FileHandle);
return TextBuffer;
}

SCDLLName("ReadLineFromFile")
SCSFExport scsf_ReadLineFromFile(SCStudyGraphRef sc)
{

  SCSubgraphRef Line1         = sc.Subgraph[0];
  SCInputRef PathAndFile      = sc.Input[0];
  
if(sc.SetDefaults)
{
sc.GraphName="Read Line Value From File";
sc.StudyDescription="Read Line Value From File";
sc.DrawZeros = 0;
sc.AutoLoop = true;
sc.GraphRegion = 0;
    sc.ValueFormat = 4;
    sc.MaintainAdditionalChartDataArrays = 1;    
    sc.CalculationPrecedence = LOW_PREC_LEVEL;    
    
    Line1.Name = "Line 1";
    Line1.DrawStyle = DRAWSTYLE_LINE;
    Line1.PrimaryColor = COLOR_CYAN;
    Line1.DrawZeros = false;
  
   PathAndFile.Name = "Path and Filename";
    PathAndFile.SetPathAndFileName("");

return;
}

    SCString PathAndFileName = PathAndFile.GetPathAndFileName();
    
    static SCString MyInputString("");
    // has to be static, otherwise the content will be cleared on very next loop

    MyInputString.Format(ReadTextFile(sc, PathAndFileName).GetSubString(ReadTextFile(sc, PathAndFileName).GetLength()-1, 0));
    
    // read the string and convert to float
    Line1[sc.Index] = strtof(MyInputString,NULL);
}

Date Time Of Last Edit: 2022-05-21 01:49:09