I have this code below and I am reading from a file named "Main.txt".
The file contain these rows:
ABC,BBB,01/04/2001,01/04/2001,1535
ABC,BBB,01/04/2001,01/04/2001,1535
ABC,BBB,01/04/2001,01/04/2001,1535
Now I want to put the last value that is 1535 to a file while I read the file "Main.txt". I will put these values to a new file that I have called "MainResult.txt".
When I run this code, only a "0" is written to the new file "MainResult.txt".
What I am after is:
1535
1535
1535
What could be wrong in this code ?
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;
}