Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello everyone. You know how in c++ if you use std::ofstream to otput to a file you have to refresh it every time the program executes to see any changes. So, the question is, is it possible to write to a file and kinda see in live how the contents are changing (in other words, automatically updating the file while writing to it) and how can I do it? Thank you very much in advance.

What I have tried:

std::ofstream to output to a text file.
Posted
Updated 16-Jan-19 8:42am

The easiest way is to check the file time and when changed read the file and detect changes.

There are different tools and frameworks out, but the QFileSystemWatcher looks interesting AND portable. For Windows you may Obtaining Directory Change Notifications.

Dont ferget that its work for the operating system of the computer and so may hurt performance.

May in the written file can placed some time stamps for identification of change time. For log file deleting old content or creating files for every day is common practise.
 
Share this answer
 
Comments
Grigorii Mordkovich 16-Jan-19 6:19am    
Thank for your suggestion.
That would depend on your OS. If you have linux or MacOS, then use tail -f. If you are on windows and have PowerShell then "Get-Content filenamehere -Wait -Tail 30" might work if you have a recent enough PowerShell version. If not google "windows tail command", and see if anything there works for you.
 
Share this answer
 
Comments
Grigorii Mordkovich 16-Jan-19 6:19am    
Thank you very much. I ll try this.
Another option is to use a memory-mapped file. I know the Windows API has this and I believe is is available in Linux also.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900