菜鸟笔记-从一个文件读取日期07/21/2016,转换为July 21,2016并输出到屏幕上

小结:很笨的原始方法

#include<iostream>
#include<fstream>

using namespace std;


int main()
{	
	ifstream file1("text.txt");
	char a[20],ch;
	int i=0;
	while(file1.get(ch)){//文件中读取的数据存入数组 
		cout<<ch;
		a[i]=ch;
		i++;
	}
	file1.close();
	cout<<endl;
	string mon[12]={"Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
	int month,day,year;
	int j;
	//cout<<a[1]-'0'<<endl;
	for(j=0;j<i;j++){
		if(a[j]=='/'){
			month=(a[j-1]-'0')+(a[j-2]-'0')*10;
			break;
			//cout<<a[j-1]-'0'<<","<<a[j-2]-'0'<<endl;
		}
			
	}
	//cout<<month<<endl;
	for(int k=j+1;k<i;k++){
		if(a[k]=='/'){
			day=(a[k-1]-'0')+(a[k-2]-'0')*10;
			//cout<<a[k-1]-'0'<<","<<a[k-2]-'0'<<endl;
		}
			
		if(a[k+1]=='\0')
			year=(a[k]-'0')+(a[k-1]-'0')*10+(a[k-2]-'0')*100+(a[k-3]-'0')*1000;
	}
	cout<<mon[month-1]<<" "<<day<<","<<year;
	return 0;
}

 

 

找到一个很好的 https://blog.csdn.net/qq_32925781/article/details/79423803 参考

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main(){

    ifstream is("/Users/.../MyDate.txt");

    if (!is){
        cerr << "File cannot be opened!" << endl;
        exit(EXIT_FAILURE);
    }

    string myMonth[13] = {"","Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec"};
    string  line;
    int month;
    int day;
    int year;

    while (is >> line) {
        month = stoi(line.substr(0,2));
        day = stoi(line.substr(3,2));
        year = stoi(line.substr(6,4));

        cout << setw(4) << left  << setfill(' ') << myMonth[month] << " ";
        cout << setw(2) << right << setfill('0') << day << "," << year << endl;
    }

    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值