Skip to content

Instantly share code, notes, and snippets.

@toanalien
Created June 4, 2015 18:57
Show Gist options
  • Save toanalien/a0c52d8169a2e5ef7e8c to your computer and use it in GitHub Desktop.
Save toanalien/a0c52d8169a2e5ef7e8c to your computer and use it in GitHub Desktop.
txt to csv
/**
* A part of contact.txt contents like
*
* Last name: nguyen van a
* General phone: 0123456789
*
* First name: nguyen thi b
* General phone: 09999999
*
* First name: tran van c
* General mobile: 012458793
* General phone: 123354789
*
*/
#include "TXTtoCSV.h"
#include <fstream>
#include <string>
using namespace std;
void TXTtoCSV::readFile()
{
string name, mobile, phone;
string result;
string line;
fstream myfile;
myfile.open("contact.txt",ios::in);
if (myfile.is_open())
{
while (!myfile.eof())
{
getline(myfile, line);
if (line.length() == 0)
{
result = name + "," + name + ",,,,,,,,,,,,,,,,,,,,,,,,,* My Contacts,Mobile," + mobile + ",Work," + phone;
this->writeFile(result);
name = "";
mobile = "";
phone = "";
continue;
}
if (line.find("Last") != std::string::npos)
name = line.substr(11, line.length() - 11);
else if (line.find("First") != std::string::npos)
name = line.substr(12, line.length() - 12);
else if (line.find("phone") != std::string::npos)
phone = line.substr(15, line.length() - 15);
else if (line.find("mobile") != std::string::npos)
mobile = line.substr(16, line.length() - 16);
}
}
myfile.close();
}
void TXTtoCSV::writeHead()
{
ofstream myfile;
myfile.open("contact.csv", ios::out);
myfile << "Name,Given Name,Additional Name,Family Name,Yomi Name,Given Name Yomi,Additional Name Yomi,Family Name Yomi,Name Prefix,Name Suffix,Initials,Nickname,Short Name,Maiden Name,Birthday,Gender,Location,Billing Information,Directory Server,Mileage,Occupation,Hobby,Sensitivity,Priority,Subject,Notes,Group Membership,Phone 1 - Type,Phone 1 - Value,Phone 2 - Type,Phone 2 - Value"<<endl;
}
void TXTtoCSV::writeFile(string str)
{
ofstream myfile;
myfile.open("contact.csv", ios::out | ios::app);
myfile << str<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment