Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 1,089 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
reading an economic indicator file line by line #454871
09/26/15 16:40
09/26/15 16:40
Joined: Apr 2015
Posts: 26
T
trader6363 Offline OP
Newbie
trader6363  Offline OP
Newbie
T

Joined: Apr 2015
Posts: 26
I have a file with a list of economic indicators and their values that I would like to read line by line and assign values from each line into variables. Problem is that while each line has identical number of variables and identical order of variables, they are of different length and some are not values but strings.
Each line has values for date and time, name of indicator, name of asset, impact of indicator and the values for forecast, previous and actual. I am able to use the scanf function to easily grab the dates and assign them to variables since they are in standard format, however I am unable to get the rest of the data like the name of the asset and name of the indicator using the scanf function. I tried using strtok to further break the line apart using delimiter but had no success assigning to variables. Any suggestions?

Here is what the data looks like:

year-mm-dd,hh:mm,asset,indicator name, impact, actual, forecast, previous
2006-12-31,00:01,AUD,Bank Holiday <<AllDay>>,L,11,2,33
2006-12-31,00:01,CNY,Trade Balance,L,4,54,6
2006-12-31,00:01,JPY,Unemployment Claims,H,74,8,97
2006-12-31,00:01,NZD,Bank Holiday <<AllDay>>,L,10,11,124

Here is what I got so far:
string Nasset, Nname, Nvalue, NewsContent;
var NYear, NMonth, NDay, NHour, NMinute, Nactual, Nforcast, Nprevious;
string NewsFile = "History\\FF calendar news events.txt";
string NewsLineContent;

string readNewsLine(string NewsLineContent){
char* line = strtok(NewsLineContent,",");
if(!line) return 0;
printf("\n%s", line);
return line+strlen(line)+1;
}

string readNews(string NewsContent){
char* line = strtok(NewsContent,"\n"); // separate news file into lines
if(!line) return 0;
NewsLineContent = line;
sscanf(line,"%4d-%2d-%2d,%2d:%2d", &NYear, &NMonth, &NDay, &NHour, &NMinute); // assign dates to variables
while(NewsLineContent) NewsLineContent = readNewsLine (NewsLineContent); // further read each line to separate using delimiters to get rest of data
return line+strlen(line)+1;
}

function main(){
NewsContent = file_content(NewsFile);
while(NewsContent) NewsContent = readNews(NewsContent); // read news file line by line
}

To reiterate I am able to assign the date and time from each line to variables, but I am unable to get the rest of the data using the scanf function. I would like to break down each line by delimiter and then assign first part to variable 1, then second part to variable 2 ... until all parts of the line are assigned. And then repeat with the next line.

Re: reading an economic indicator file line by line [Re: trader6363] #454907
09/28/15 21:41
09/28/15 21:41
Joined: Apr 2015
Posts: 26
T
trader6363 Offline OP
Newbie
trader6363  Offline OP
Newbie
T

Joined: Apr 2015
Posts: 26
To simplify the above, basically I would like to extract variables delimited with a comma in a line that I pull from a file.
So if a line is: 2006-12-31,00:01,AUD,Bank Holiday <<AllDay>>,L,11,2,33
I would like to have
variable 1 = 2006-12-31
variable 2 = 00:01
variable 3 = AUD
Variable 4 = Bank Holiday <<AllDay>>
... and so on for everything within that line. And then I would like to read the next line and assign variables from that new line and so on..

Re: reading an economic indicator file line by line [Re: trader6363] #454929
09/29/15 18:14
09/29/15 18:14
Joined: Apr 2015
Posts: 26
T
trader6363 Offline OP
Newbie
trader6363  Offline OP
Newbie
T

Joined: Apr 2015
Posts: 26
Ok figured it out. It can be done using the scanf function. You define a character array and then within the scanf function you use the following format %array length[^delimit] and you dont use a pointer sign in front of the char array name.
For example above scanf function would be:
sscanf(line,"%4d-%2d-%2d,%2d:%2d,%3[^,],%30[^,],%1[^,],%d,%d,%d", &NYear, &NMonth, &NDay, &NHour, &NMinute, Nasset, Nname, Nvalue, &Nactual, &Nforcast, &Nprevious);


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1