Login Page - Create Account

Support Board


Date/Time: Fri, 18 Apr 2025 07:03:51 +0000



Post From: Read & parse CSV

[2025-04-04 04:22:08]
drinkcodejava - Posts: 27
Here is a snippet of sample code that works for SCString::ParseLines() and SCString::Tokenize().

std::vector<SCString> Lines;
std::vector<float> FloatVals;
std::vector<char*> Tokens;

dataString.ParseLines(Lines); // dataString is an SCString that is data read from a file
for (SCString line : Lines) {
  msg.Format("Line as string: %s", line.GetChars());
  sc.AddMessageToLog(msg,0);
  
  // msg.Format("Float values: ");
  // line.ParseLineItemsAsFloats(FloatVals);
  // for (float value : FloatVals) {
    // msg.AppendFormat("%.2f, ", value);
  // }
  // msg.AppendFormat("FloatVals size: %d", FloatVals.size());
  // FloatVals.clear();
  
  msg.Format("Tokens: ");
  line.Tokenize("\t", Tokens);
  for (char* p_Char : Tokens) {
    msg.AppendFormat("%s, ", SCString(p_Char).GetChars());
  }
  msg.AppendFormat("Tokens size: %d", Tokens.size());
  
  sc.AddMessageToLog(msg,0);
}

There are a few lines commented out for ParseLineItemsAsFloats(). For this function to work, dataString must be comma-separated values (with or without whitespace after each comma). Text gets parsed into a 0 value. There is something strange with the resulting FloatVals vector though; the .size() return value seems to be 1 less than the actual. Weird.

Here is sample data:
Date  Time  Open  High  Low  Close  Vol
2025-04-03  00:33:00  -161  225  -221  63  0
-161.01  225.5  -221.99  63.241  0.05

And here is the output (for the above code, as-is):
Line as string: Date  Time  Open  High  Low  Close  Vol
Tokens: Date, Time, Open, High, Low, Close, Vol, Tokens size: 7
Line as string: 2025-04-03  00:33:00  -161  225  -221  63  0
Tokens: 2025-04-03, 00:33:00, -161, 225, -221, 63, 0, Tokens size: 7
Line as string: -161.01  225.5  -221.99  63.241  0.05
Tokens: -161.01, 225.5, -221.99, 63.241, 0.05, Tokens size: 5

If you need help figuring out how to get sc.ReadFile() working, there are some working examples here: How to read file with sc.ReadFile()?