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:
I haven't tried it yet, but see another problem in your CSV file: The "MKT CLOSE" lines contain no aux price, but the script tries to read a nonexistent price.
Don't attempt to program everything at once. Write a program step by step, and test every step. In this case, first write a simple version that only reads a single line. For this you can just comment out the while loop. If that works, extend it to reading multiple lines, then check the "MKT CLOSE" case.
Edit: the printf line has also a problem, you must cast float to (var) for printing it.
Re: Using string functions to read in the contents of a CSV file
[Re: jcl]
#454096 08/27/1510:3908/27/1510:39
I have almost got this working, but have hit the point where I need some more help. If anyone can suggest a solution or see where I'm going wrong, I'd greatly appreciate some advice.
My CSV data's format has changed slightly: AUD/USD,Buy 0.04 Lots at 0.7213 Stop.,- IF DONE -,Sell 0.04 Lots at 0.7113 Stop - OCO - MOC. AUD/USD,Sell 0.04 Lots at 0.7023 Stop.,- IF DONE -,Buy 0.04 Lots at 0.7123 Stop - OCO - MOC. EUR/USD,Buy 0.02 Lots at 1.1625 Stop.,- IF DONE -,Sell 0.02 Lots at 1.1444 Stop - OCO - MOC. EUR/USD,Sell 0.02 Lots at 1.1220 Stop.,- IF DONE -,Buy 0.02 Lots at 1.1401 Stop - OCO - MOC. USD/JPY,Buy 0.03 Lots at 121.6742 Stop.,- IF DONE -,Sell 0.03 Lots at 120.0531 Stop - OCO - MOC. USD/JPY,Sell 0.03 Lots at 116.9908 Stop.,- IF DONE -,Buy 0.03 Lots at 118.6119 Stop - OCO - MOC.
I am now able to grab all of the values I need with the exception of the stop value in every second row (that is, the value before the phrase "Stop - OCO - MOC." in every second row).
I've got to say, I am at my wits end with this! I just can't work out why my code parses all the values except every other row's stop. I think it must be something to do with the data itself, but I can't see what. Here's my code:
And the output of the printf - each row should reflect a row in the original CSV, but the second number is not updated. [1: Mon 04.01.10 00:00] 83.150-83.120-83.230-83.170 AUD/USD, 0.721300, 0.711300 AUD/USD, 0.702300, 0.711300 EUR/USD, 1.162500, 1.144400 EUR/USD, 1.122000, 1.144400 USD/JPY, 121.674202, 120.053101 USD/JPY, 116.990799, 120.053101 GBP/USD, 1.563300, 1.552500 GBP/USD, 1.548500, 1.552500 USD/CAD, 1.341800, 1.328500 USD/CAD, 1.321800, 1.328500 EUR/JPY, 137.117203, 135.667801 EUR/JPY, 135.667801, 135.667801
strtok is not looking for the separator, but for the text "separator", and of course does not find it. This causes your line parsing becoming out of sync at every other line.
You can find out such issues by looking at the parameters, f.i.
string s = strtok(0, " "); //"at" printf("\n%s",s);
If it does not print "at", something went wrong before.
Re: Using string functions to read in the contents of a CSV file
[Re: jcl]
#454102 08/27/1514:5608/27/1514:56