File.Open Truncates File
bhalpin
Member Posts: 309
Hi.
To facilitate debugging, I have a codeunit with functions to open/create a 'log' file, write to the file, and close the file. I have two problems:
1 - After the file is opened/created, each call to the write function seems to truncate the file to zero length, then the text is written. The result is I never have anything in the file except the last line I wrote.
Here is the code that opens/creates the file:
NAV documentation (typically) say's nothing about whether the file pointer is set to the start or end, whether the file is truncated, etc. And there are no options to the OPEN statement to control the behaviour (like in C.)
After fighting with it I have a solution, but it sucks:
With this in place, I get all the output as expected.
But, surely I've missed something ...
2 - The code above also solves this problem, but ...
Without using any close/open tricks like above, logging of code progress to a text file seems to be useless if the code fails with a C/Side error. In this case, database changes are rolled back - (which I somethimes wish it wouldn't) AND any text file being written to is rolled back (ie deleted) as well. So I have no debug trail to see where the code failed.
Again, have I messed something?
Thanks' in advance.
Bob
To facilitate debugging, I have a codeunit with functions to open/create a 'log' file, write to the file, and close the file. I have two problems:
1 - After the file is opened/created, each call to the write function seems to truncate the file to zero length, then the text is written. The result is I never have anything in the file except the last line I wrote.
Here is the code that opens/creates the file:
LogFile.TEXTMODE(TRUE); LogFile.WRITEMODE(TRUE); IF NOT LogFile.OPEN(LogFileName) THEN LogFile.CREATE(LogFileName);
NAV documentation (typically) say's nothing about whether the file pointer is set to the start or end, whether the file is truncated, etc. And there are no options to the OPEN statement to control the behaviour (like in C.)
After fighting with it I have a solution, but it sucks:
LogFile.WRITE(ParameterValue); LogFile.CLOSE; LogFile.OPEN(LogFileName); LogFile.SEEK(LogFile.LEN);
With this in place, I get all the output as expected.
But, surely I've missed something ...
2 - The code above also solves this problem, but ...
Without using any close/open tricks like above, logging of code progress to a text file seems to be useless if the code fails with a C/Side error. In this case, database changes are rolled back - (which I somethimes wish it wouldn't) AND any text file being written to is rolled back (ie deleted) as well. So I have no debug trail to see where the code failed.
Again, have I messed something?
Thanks' in advance.
Bob
0
Comments
-
-
Thanks, I had seen those posts when I search the forum before adding mypost.
I guess this all just confirms that the only way to append to a file is to open it and then seek to the end before you write to it. Weird, but ... whatever.
Bob0
Categories
- All Categories
- 75 General
- 75 Announcements
- 66.7K Microsoft Dynamics NAV
- 18.8K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 610 NAV Courses, Exams & Certification
- 1.9K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 251 Dynamics CRM
- 103 Dynamics GP
- 6 Dynamics SL
- 1.5K Other
- 991 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 28 Design Patterns (General & Best Practices)
- Architectural Patterns
- 9 Design Patterns
- 4 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1K General Chat
- 1.6K Website
- 77 Testing
- 1.2K Download section
- 23 How Tos section
- 249 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions