0 registered members (),
1,209
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
reading an economic indicator file line by line
#454871
09/26/15 16:40
09/26/15 16:40
|
Joined: Apr 2015
Posts: 26
trader6363
OP
Newbie
|
OP
Newbie
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]
#454929
09/29/15 18:14
09/29/15 18:14
|
Joined: Apr 2015
Posts: 26
trader6363
OP
Newbie
|
OP
Newbie
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);
|
|
|
|