1.格式转换,从一个文件中读取日期07/21/2016,转换为以下格式July 21,2016并输出到屏幕上
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;
string monthtab[13]={"","January","February","March","April","May","June","July","August","Sepetember","October","November","December"};
string ctos(char *s)
{
stringstream ss;
ss<<s;
string newstr;
ss>>newstr;
return newstr;
}
int stoi(string str)
{
stringstream ss;
ss<<str;
int number;
ss>>number;
return number;
}
void transform(string str)// 07/21/2016
{
int pos1;
string month;
pos1=str.find_first_of('/',0); // 找到第一个出现的位置
string t = str.substr(0,pos1);
month=monthtab[stoi(t)];
cout<<month<<" ";
str.erase(0,pos1+1);
pos1=str.find_first_of('/',0); // 替换后面字符串中的,号
str.replace(pos1,1,",");
cout<<str<<endl;
}
int main()
{
char buf[20];
ifstream infile("D:\\mytest8.txt");
if(!infile.is_open())
{
cout<<"file error"<<endl;
}
else
{
infile.getline(buf,20);
cout<<buf<<endl;
transform(ctos(buf));
}
infile.close();
return 0;
}