Prompt the code that will issue a message before exiting the program, that changes have been made to the file, with a request to save.

PS Text editor.

  • just keep track of your editor’s work if the text has changed, including with regard to canceled actions. Specific code is meaningless to give here - DreamChild
  • With my minimal, if I may say so, knowledge is better explained by example of code. - Korol_tref
  • Are you suggesting that I write you a text editor and save the changes? - DreamChild
  • No, only the exit function, with its conditions, if the file has been changed, then save / cancel / go back to the text. I wrote the main editor, with difficulty, but myself. - Korol_tref

1 answer 1

Algorithm like this.

Start a flag that indicates whether there were any changes in the file. When loading a document, the flag should be cleared (that is, it is false ) - it’s quite logical that you barely opened the document and you haven’t changed anything in it.

Further, it should be remembered that the flag can be cleared or set in case of any changes to the document. There can be four ways to change it (correct it if I am mistaken):

  1. change by typing / deleting text from the keyboard;
  2. changes by inserting-cutting text;
  3. text formatting changes;
  4. changes by undoing / reverting previous changes.

The first three types of changes should set the flag to true - they all assume that the received text will be different from the original one. However, the fourth type of change (undo / redo of previous changes) implies that the document can be returned to its original state. In this case (if of course you have Undo / Redo provided in the editor), you will need to memorize the steps, more precisely, the step that returns the document to its original state. / Upon reaching this step, the flag is also reset to false . For all other steps, the flag will be equal to true.

Well, the last thing to mention is that the flag should be reset when the document is saved. I will not give the code - it will take too much of it to correctly demonstrate the operation of this mechanism. But in general, everything is quite simple.

ZY A very bad idea is a head-on solution - to compare the current contents of a file with the old one at the slightest change. If with small document sizes this can somehow work, in the case of large documents serious brakes can occur.