{"id":92,"date":"2011-09-22T11:27:41","date_gmt":"2011-09-22T11:27:41","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=92"},"modified":"2011-09-28T16:03:17","modified_gmt":"2011-09-28T16:03:17","slug":"tail-f-functionality-in-standard-c","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2011\/09\/tail-f-functionality-in-standard-c\/","title":{"rendered":"tail -f like functionality in standard C++"},"content":{"rendered":"<p>For those who don&#8217;t know tail -f monitors the new lines as they are added to a file in UNIX like systems. It&#8217;s a useful way of monitoring a logfile. I wanted to do something similar in C++, however it looks like you need platform specific language extensions to do this is a reasonable way (inotify on Linux). So I created the following quick and dirty hack. This hack maybe slow if you have large files, with few linefeeds, but it suits my purposes.<\/p>\n<p>#include <iostream><br \/>\n#include <string><br \/>\n#include <fstream><\/p>\n<p>using namespace std;<\/p>\n<p>int find_last_linefeed(ifstream &#038;infile) {<\/p>\n<p>  infile.seekg(0,ios::end);<br \/>\n  int filesize = infile.tellg();<\/p>\n<p>  for(int n=1;n<filesize;n++) {\n    infile.seekg(filesize-n-1,ios::beg);\n\n    char c;\n    infile.get(c);\n\n    if(c == 0x0A) return infile.tellg();\n  }\n}\n\n\nint main() {\n\n\n  int last_position=-1;\n  for(;;) {\n\n    ifstream infile(\"testfile\");\n    int position = find_last_linefeed(infile);\n\n    if(position > last_position) {<br \/>\n      infile.seekg(position,ios::beg);<br \/>\n      string in;<br \/>\n      infile >> in;<br \/>\n      cout << in << endl;\n    }\n    last_position=position;\n\n    sleep(1);\n  }\n\n}\n[\/sourcecode]\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For those who don&#8217;t know tail -f monitors the new lines as they are added to a file in UNIX like systems. It&#8217;s a useful way of monitoring a logfile. I wanted to do something similar in C++, however it looks like you need platform specific language extensions to do this is a reasonable way [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-92","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-1u","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/92","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=92"}],"version-history":[{"count":9,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/92\/revisions"}],"predecessor-version":[{"id":204,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/92\/revisions\/204"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=92"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=92"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=92"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}