Thread: Refreshing a Text file in GUI application?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    Refreshing a Text file in GUI application?

    Was wondering if anyone could enlighten me on how to refresh an open file inside a gui editor.

    I have a fairly simple program coded that uses WNDCLASSEX to setup the MDIChildWindow. (This is a text editing program that has the ability to open multiple childs.)

    What I want is a way to, SIMPLY, refresh any open text files if the original file has been changed via another user/program/etc.

    It is not neccessary that the code use API to check to see if the file has been modified. Meaning, I would be happy with some sort of timer control that every x interval reloaded the file. (of course in the same Child Window, e.g. I would not want it to open a new window for the update file.)

    In summation, I guess what I am inquiring on is something that works like a log file viewer(real time) or if anyone has used notepad++, notepad++ has a feature that if the file has changed it prompts you with "File has changed would you like to view new changes." Of course, the prompt is not needed.

    I can post my code if requested, but will refrain for now in order to not pyramid the thread.

    Thanks

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    One simple way of doing this would be to constantly check the file size and reread the file when it changes. The primary disadvantage to that method is that you would get no refresh if someone altered the file but the end result was something the same size as it previously was.

    You could also use file mapping and just track changes, but this wouldn't be simple. It would help to know what OS you are writing the program for.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    If you wish to poll for changes, you can use GetFileTime(). You will know the file has changed when the lpLastWriteTime value changes.

    If you wish to be notified of changes, you can use FindFirstChangeNotification with FILE_NOTIFY_CHANGE_LAST_WRITE. This will be triggered if a change is made to any file in the monitored directory. You then use GetFileTime() to check if it is your file that has changed. You could use something like the following pseudo-code in a seperate thread:
    Code:
    void MonitorForChanges(LPCTSTR szFile)
    {
    	HANDLE hChange = FindFirstChangeNotification(PathOfFile(szFile), FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE);
    
    	while (TRUE)
    	{
    		WaitForSingleObject(hChange, INFINITE);
    
    		GetFileTime(hFile, &ft1, &ft2, &ftWrite);
    
    		if (CompateFileTime(&ftWrite, &ftOriginal) != 0)
    		{
    			// Your file has changed. Reload
    		}
    
    		FindNextChangeNotification(hChange);
    	}
    }
    Once you have detected a change, you can update your text box using SetWindowText().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM