I have tried this testcode out below to search for the date "01/03/1999" in my file that consist of these lines:
01/02/1999,1,2,3,4,5
01/02/1999,1,2,3,4,5
01/03/1999,1,2,3,4,5
01/03/1999,1,2,3,4,5
01/04/1999,1,2,3,4,5
01/04/1999,1,2,3,4,5
#include <iostream>
#include <fstream>
#include "datefile.hpp"
using namespace datefile;
ifstream ReadFile("C:\\Folder1\\File1.txt");
time_t date = string_to_date( "01/03/1999" );
streampos linepos = find_date( ReadFile, date);
if( ReadFile )
{
string line;
getline( ReadFile.seekg( linepos ), line );
String^ FoundLine = gcnew String(line.c_str());
MessageBox::Show(FoundLine);
}
When running the code that are inside a buttoncontrol, I will have an emty message in the MessageBox.
From what I can understand, ´streampos linepos´ will find the right position where the date "01/03/1999" is and later I will with getline seekg linepos, wich should be in the beginning at the line where the date was found and from here get this whole line ?
Then I put this ´line´ to a messageBox but here the messageBox show an emty message.
I am not sure if I have missed any detail.
Alright! :)
Sorry for the delay. There were a few more corner cases than I thought there would be when I started this. But anyway, here you go. Hope you find it useful.
The algorithm presumes that the dates in your file are more or less linearly distributed. If this is not the case by more than a standard deviation, open "datefile.cpp" and comment out the line
#define LINEAR_DISTRIBUTION
Sorry for the boilerplate, but companies get nervous without it. Basically it just says you can do anything you like with the files except claim anyone but I wrote the original versions or alter the boilerplate... (and excludes me from legal responsibility if someone manages to destroy something with it).The file "a.cc" is what I used to test the datefile algorithm. You don't need it, but I've attached it anyway so you can play with it if you like. Its messy though...
Let me know if anything goes horribly wrong. ;)
So, here's a quick primer:
#include <iostream> #include <fstream> #include "datefile.hpp" using namespace std; using namespace datefile; int main() { ifstream megafile( ... ); time_t date = string_to_date( "4/28/1974" ); streampos linepos = find_date( megafile, date ); if (megafile) // or (!megafile.eof()) { string line; getline( megafile.seekg( linepos ), line ); cout << "Found the line> " << line << endl; } else { megafile.clear(); cout << "No such date found.\n"; } ... megafile.close(); return 0; }
Enjoy! :)
Hmm. OK. A while back a server failure stuffed my inbox with thousands of messages. Since I can only delete 25 at a time I've given up.
Apparently this is also preventing me from attaching files. So I'll double-post them into the next post...