Posted By: boatman
Using string functions to read in the contents of a CSV file - 08/24/15 07:46
I'm trying to read in the contents of a CSV file and use those contents to set up certain parameters in a script. I've modeled my script after the example on the Data Import/Export page in the manual - Data Import-Export - but am having trouble with the sscanf function. I would like to read the floating point numbers in the 4th and 6th columns, but my script seems to return values of 0 and results in Error 111: Crash in script. I have set the data type to float as per the manual.
Running Zorro in diagnostics mode doesn't reveal the reason for the crash,but when I comment out the sscanf lines, the crash doesn't happen.
Any suggestions? I have just started learning about string operations in C, so the error might be obvious to an advanced user.
Here is my script and CSV file:
Running Zorro in diagnostics mode doesn't reveal the reason for the crash,but when I comment out the sscanf lines, the crash doesn't happen.
Any suggestions? I have just started learning about string operations in C, so the error might be obvious to an advanced user.
Here is my script and CSV file:
Code:
string Name = "Data\\tradeList.csv"; // name of the CSV file string readTrade(string csv, string* tAsset, string* tType, float* tLots, float* tStop ) { string nextline = strstr(csv,"\n"); if(nextline) nextline++; string separator = strstr(csv,","); if(separator) separator = ","; else separator = ";"; *tAsset = strtok(csv,separator); //symbol if(!*tAsset) return nextline; strtok(0,separator); // expiry *tType = strtok(0,separator); // action: buy or sell sscanf(strtok(0,separator),"%f", &tLots); //quantity in standard lots strtok(0,separator); // order type sscanf(strtok(0,separator),"%f", &tStop); //aux price return nextline; } function run() { set(LOGFILE); // StartDate = STARTDATE; LookBack = 0; if(is(LOOKBACK) || is(TRADING)) return; if(!file_date(Name)) quit("File not found!"); string content = file_content(Name); string tAsset = "",tType = ""; float tLots; float tStop; while(content) { content = readTrade(content,&tAsset,&tType,&tLots,&tStop); printf("\n%s %s %f %f", tAsset, tType, tLots, tStop); } }