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...

You've got it.

You are saying that you actually see the messagebox? (I didn't get that far...)

It seems my CRLF handling (*cough*) wasn't good enough (*cough*). When I wrote it I tested using a stringstream, but apparently it did something to the CRLF I didn't expect, so it appears that you'll have to open your file in binary mode after all... Sorry about that.

I'll play with fixing that, but in the meantime you'll just have to strip any '\r' off the end of the line manually:

#include <iostream>
#include <fstream>

using namespace std;
using namespace datefile;

ifstream ReadFile("C:\\Folder1\\File1.txt", ios::binary );  // Binary mode

time_t date = string_to_date( "01/03/1999" );
streampos linepos = find_date( ReadFile, date);

 if( ReadFile )
 {
      string line;
      getline( ReadFile.seekg( linepos ), line, '\r' );  // stop at CR of CRLF

      String^ FoundLine = gcnew String(line.c_str());
      MessageBox::Show(FoundLine);
 }

I'm so sorry... :$

Don´t be sorry :pretty:
This is really working now. I have implemented this in my project and it works more than nice. This was an amazing function really. I have to thank you 1 million times :)
The search for the date goes very fast and it works great.
Thanks, thanks.

/J :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.