Originally Posted by
Coding
Todd Burch,
The solution with string.rfind() and string.substr() sounds interesting. Still I have the problem.
I have never used these functions though and have a little problem to implement them in the code. I have tried but not really get it right.
If you have the time I would appreciate some assistance how an approch could look like for this code.
The line in the file look like this:
ABC,BBB,01/04/2001,01/04/2001,1535
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main ()
{
std::string Name;
std::string Action;
std::string BDate;
std::string SDate;
char Comma;
int BTime = 0;
ofstream MainResult;
MainResult.open ("MainResult.txt");
ifstream Main ("Main.txt");
while ( getline(Main, Name, ',') )
{
Main >> Action;
Main >> Comma;
Main >> BDate;
Main >> Comma;
Main >> SDate;
Main >> Comma;
Main >> BTime;
Main.get(); // read in trailing newline character
MainResult << BTime << "\n";
} // End Whilestatement
return 0;
}
Isn't this where you started? Why do you insist on using >> when everyone here, and your own experiments, tell you it doesn't do what you want? You may believe that >> stops at commas, but it does not. Again, to read up to the comma, use getline -- as you have it in your program! --
Code:
getline(Main, Action, ',');
getline(Main, BDate, ',');
et cetera. Note as well that getline finds the comma and throws it away for you.
Todd's idea is simpler, but will only find the last part of the line. If you will never need Action and Date and all the rest, then there's no reason to try to read them. If you do need them, or will in the future, then you'll have to parse through the line as above.