[ create a new paste ] login | about

Link: http://codepad.org/SnOfwtdV    [ raw code | output | fork ]

C++, pasted on Jan 6:
// csvshow.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <list>

using namespace std;

 void split(  
     const std::string& str,   
     const std::string& delim,   
     std::list<std::string>& result)  
 {  
     std::string::size_type offset = 0;  
     std::string::size_type pos = 0;  
     std::string::const_iterator begin = str.begin();  
     std::string::const_iterator end = str.end();  
   
     while((pos = str.find_first_of(delim, offset)) != str.npos)  
     {  
         if(offset != pos)  
         {  
             result.push_back(std::string(begin + offset, begin + pos));  
         }  
         offset = pos + 1;  
     }  
     result.push_back(std::string(begin + offset, end));  
 }

 int _tmain(int argc, _TCHAR* argv[])
{
	ifstream ifs("sample.csv");
	ofstream oddofs("odd.csv");
	ofstream evenofs("even.csv");

	list<list<string>> odds,evens;

	for(int i = 0; ifs ; i++)
	{
		string line;
		getline(ifs,line);
		
		list<string> tokens;
		split(line,",",tokens);

		if(i % 2 == 0)
		{
			odds.push_back(tokens);
			oddofs << line << endl;
		}
		else
		{
			evens.push_back(tokens);
			evenofs << line << endl;
		}
	}
	ifs.close();
	oddofs.close();
	evenofs.close();

	ofstream ofs("diff.csv");

	list<list<string>>::iterator oddsit = odds.begin();
	list<list<string>>::iterator evensit = evens.begin();
	while(oddsit != odds.end() && evensit != evens.end())
	{
		list<string>::iterator oddit = oddsit->begin();
		list<string>::iterator evenit = evensit->begin();
		while(oddit != oddsit->end() && evenit != evensit->end())
		{
			int num1 = atoi(oddit->c_str());
			int num2 = atoi(evenit->c_str());

			int sum = num1 - num2;

			char strnum[100];
			sprintf(strnum,"%i,",sum);

			ofs << strnum;

			oddit++;
			evenit++;
		}
		ofs << std::endl;

		oddsit++;
		evensit++;
	}
	ofs.close();

	return 0;
}


Output:
1
2
3
Line 19: error: stdafx.h: No such file or directory
Line 33: error: '_TCHAR' has not been declared
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: