Working with files

An important part of working with files. In this entry I will list some points in working with csv file. About csv file you can refer this.

To work with files in C++, we need libraries – fstream that has ofstream adn ifstream classes. Let’s start with a small project using most basis steps.

Description of the project: There is a csv file with adjust price close of 5 different stocks (let’s say “AAPL”,” GOOG”,”BP”,” IBM”,”TM”) for a month. The requirement is record by dates the most expensive and the cheapest stock by the following format:

20160701,AAPL,302.1,TM,123.4
20160701,GOOG,304.1,BP,120.1

Let’s start with pseudo code of the project:

  1. Open input file
  2. Recognize stock symbols
  3. Read each day data into a string vector
  4. Compare adjust close price of 5 stocks, get the most expensive and cheapest one
  5. Record information into an output file

There is the code of this project:

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
// function to record a row of csv file into a vector
std::vector<std::string> CsvRowToVector(std::string strInput){
	std::vector<std::string> _t;
	if (!strInput.empty()){
		do{
			if (strInput.find(",")!=std::string::npos){
				_t.push_back(strInput.substr(0,strInput.find(",")));
				strInput=strInput.substr(strInput.find(",")+1);
			}else {
				_t.push_back(strInput);
				strInput.clear();
			}
		}while(strInput.size()!=0);
	}
	return _t;
}
int main(){
	// declaration variables
	std::vector<std::vector<std::string> > rawData;	//store raw data from input file
	std::vector<std::vector<std::string> > result;		//store result to record output file
	std::ifstream fin;						//input file
	std::ofstream fout;						//output file
	fin.open("input.csv",std::ifstream::in);
	fout.open("output.csv",std::ofstream::out);
	std::string temp;						// temporcary string working with reading input file
	// check if files are opened properly
	if (!fin.is_open()) {std::cout<<"input file cannot be opened to read data"<<std::endl;return -1;}
	if (!fout.is_open()) {std::cout<<"output file cannot be opened to write results"<<std::endl;return -1;}
	// working with input file
	// get the first line - stock symbols
	std::getline(fin,temp);
	rawData.push_back(CsvRowToVector(temp));
	// import daily data into following rows of vector
	// than compare prices and import results to result vector
	while(std::getline(fin,temp)){
		// import
		rawData.push_back(CsvRowToVector(temp));
		//compare and get max and min Index
		unsigned maxIndex=1;
		unsigned minIndex=1;
		for(unsigned jj=1;jj<rawData[rawData.size()-1].size();++jj){ 			if (std::stod(rawData[rawData.size()-1][jj])>std::stod(rawData[rawData.size()-1][maxIndex])) maxIndex=jj;
			if (std::stod(rawData[rawData.size()-1][jj])<std::stod(rawData[rawData.size()-1][maxIndex])) minIndex=jj;
		}
		// import result to result vector
		std::vector<std::string> vTemp;
		vTemp.push_back(rawData[rawData.size()-1][0]); 	//date
		vTemp.push_back(rawData[0][maxIndex]); 			//the most expensive stock symbol
		vTemp.push_back(rawData[rawData.size()-1][maxIndex]);//the most expensive price
		vTemp.push_back(rawData[0][minIndex]); 			//the cheapest stock symbol
		vTemp.push_back(rawData[rawData.size()-1][minIndex]);//the most expensive price
		result.push_back(vTemp);
	}
	fin.close();
	std::cout<<"Reading raw data finished"<<std::endl;
	// finish to work with input file
	// record to output file
	for(unsigned jj=0;jj<result.size();++jj){
		for(unsigned ii=0;ii<result[jj].size()-1;++ii){
			fout<<result[jj][ii]<<",";
		}
		fout<<result[jj][result[jj].size()-1]<<"\n";
	}
	fout.close();
	std::cout<<"Writing results finished"<<std::endl;
	// finish to write
}

The comments can clearly explain the above code. There are some point we should remember:

  1. ifstream: stream class to read from files
  2. ofstream: stream class to write to files
  3. getline – read data by row from files
  4. << – write data to files

There is a result of executing program from the above code.

input.csv

 

DATE,AAPL,BP,GOOG,IBM,TM
12/31/2015,104.058365,30.04802,758.880005,134.926379,123.040001
12/30/2015,106.094845,30.09608,771,136.612715,123.220001
12/29/2015,107.498633,30.63437,776.599976,137.044105,124.760002
12/28/2015,105.600553,30.480572,762.51001,134.91658,124.470001
12/24/2015,106.796739,30.874675,748.400024,135.544053,125.769997
12/23/2015,107.37012,31.095758,750.309998,135.82837,127.150002
12/22/2015,106.005876,29.596243,750,135.230309,125.580002
12/21/2015,106.104733,28.96183,747.77002,132.847878,124.650002
12/18/2015,104.819571,28.981055,739.309998,132.259616,124.389999
12/17/2015,107.735899,29.173301,749.429993,134.073412,124.800003
12/16/2015,110.06895,29.673141,758.090027,136.563691,126.660004
12/15/2015,109.228655,29.557793,743.400024,135.09305,122.169998
12/14/2015,111.195943,29.048341,747.77002,133.269455,122.5
12/11/2015,111.887949,29.596243,738.869995,131.936088,123.669998
12/10/2015,114.843813,30.567082,749.460022,134.102824,124.889999
12/9/2015,114.300096,30.576695,751.609985,133.936153,123.900002
12/8/2015,116.880302,30.057632,762.369995,135.34797,122.519997
12/7/2015,116.929726,30.04802,763.25,136.818611,125.150002
12/4/2015,117.671164,31.403351,766.809998,137.681377,125.900002
12/3/2015,113.884885,31.970476,752.539978,136.200937,124.720001
12/2/2015,114.952558,32.508765,762.380005,136.965669,125.739998
12/1/2015,116.000455,33.421934,767.039978,138.514746,125.919998

output.csv

 

12/31/2015,GOOG,758.880005,BP,30.04802
12/30/2015,GOOG,771,BP,30.09608
12/29/2015,GOOG,776.599976,BP,30.63437
12/28/2015,GOOG,762.51001,BP,30.480572
12/24/2015,GOOG,748.400024,BP,30.874675
12/23/2015,GOOG,750.309998,BP,31.095758
12/22/2015,GOOG,750,BP,29.596243
12/21/2015,GOOG,747.77002,BP,28.96183
12/18/2015,GOOG,739.309998,BP,28.981055
12/17/2015,GOOG,749.429993,BP,29.173301
12/16/2015,GOOG,758.090027,BP,29.673141
12/15/2015,GOOG,743.400024,BP,29.557793
12/14/2015,GOOG,747.77002,BP,29.048341
12/11/2015,GOOG,738.869995,BP,29.596243
12/10/2015,GOOG,749.460022,BP,30.567082
12/9/2015,GOOG,751.609985,BP,30.576695
12/8/2015,GOOG,762.369995,BP,30.057632
12/7/2015,GOOG,763.25,BP,30.04802
12/4/2015,GOOG,766.809998,BP,31.403351
12/3/2015,GOOG,752.539978,BP,31.970476
12/2/2015,GOOG,762.380005,BP,32.508765
12/1/2015,GOOG,767.039978,BP,33.421934

Advertisements