jeudi 26 mars 2015

How to extract this information


I have come across questions of this type so many times in C++ that I felt it's about time I learn this technique. I know how it's done in Java, but I'm still a novice in C++.


Suppose I have a text file which contains some dates in this format:



May 3rd, 2014, /
December 4th, 2011, -
January 19th, 200, -
January 1st, 2011, /
March 3rd, 1900, /


The last char in each line is like a date separator as shown below.


How can I convert them to this format and write in another text file:



5/3/2014
12-4-2011
01-19-200
1/1/2011
03/03/1900


The focus here is on string manipulation and not file IO.


My try:



#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
ifstream in;
string s;
cout << "\nPlease enter the input file name\n";
cin >> s;
in.open(s);
if (in) {
// Logic goes here
}
else {
cout << "\nCouldn't locate the file.\n";
}
cin.get();
return 0;
}



Aucun commentaire:

Enregistrer un commentaire