Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error saving Memo Lines to File.

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
A little while back I posted an issue about wanting to insert lines into a point within a text file. I have since resorted to the plan of feeding through the first 'x' lines from the original file into a Memo, adding lines of text that I want to insert and then feeding through the remaining lines from the file. Following this I am then allowing the user to re-save the file to its original place - using the syntax 'Memo1.Lines.SaveToFile()'. This seems to operate successfully when I am running the application within the IDE. When I then try to run the same build of the application outside of the IDE I am presented with the error 'Cannot create file .....'.
I'm obviously missing something here - can anyone suggest what it is ?
I'm opening the file using 'AssignFile()' and then 'Reset()', then reading the lines using 'Readln()'. Following reading the required 'x' lines, adding lines of text into the memo and then adding the remaining lines from the file (again using 'Readln()') I am then using 'CloseFile()'. Is there some other step I need to take or am I performing the wrong steps ?
Thanks in advance
Steve
 
Check your paths, of course. You might also want to make sure that you have a try..finally so it always gets closed. Something like...

assignfile(myFile, 'filename.txt');
reset(myFile);
try
while not eof(myFile) do
readln(myFile, line);
finally
closefile(myFile);
end;
TealWren
 
I've now resolved this issue - thanks for the suggestion. It must have been the fact that the file being read from was not being closed correctly (albeit that the 'CloseFile()' command was in place.
Originally I was opening the file, reading my 'x' lines and feeding them into the memo (not closing the file), adding my own lines and then running a 'while not eof()' on the remaining lines feeding them into the memo, completing the process with a 'CloseFile()' call. Then on trying to save the lines from the memomback to the file I was presented with the error indicating that I was unable to create the file.
Following the suggestion made I closed the file in between the steps of reading the first 'x' lines and adding my own lines to the memo, before reopening it, stepping through to the 'x'th line and feeding through the remaining lines, closing the file again afterwards. This then successfully allowed me to save the file as required. The error may possibly have been due to me performing some work whilst leaving the file open before returning to it to read the remaining lines.
Thanks for the assist.
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top