Learn what to do if you can’t collect money from a customer and need to write it off as bad debt in QuickBooks Desktop. When invoices you send in QuickBooks Desktop become uncollectible, you need to record them as a bad debt and write them off. This ensures your. See more of Off The Record on Facebook. Create New Account. See more of Off The Record on Facebook. Create New Account. EnergyX Solutions Inc. On Writing & Publishing Anxiety. October 19 at 6:08 PM.
-->This article helps you read from and write to a text file by using Visual C#.
Original product version: Visual Studio
Original KB number: 816149
Original KB number: 816149
Summary
The Read a text file section of this article describes how to use the
StreamReader
class to read a text file. The Write a text file (example 1) and the Write a text file (example 2) sections describe how to use the StreamWriter
class to write text to a file.Read a text file
The following code uses the
StreamReader
class to open, to read, and to close the text file. You can pass the path of a text file to the StreamReader
constructor to open the file automatically. The ReadLine
method reads each line of text, and increments the file pointer to the next line as it reads. When the ReadLine
method reaches the end of the file, it returns a null reference. For more information, see StreamReader Class.- Create a sample text file in Notepad. Follow these steps:
- Paste the hello world text in Notepad.
- Save the file as Sample.txt.
- Start Microsoft Visual Studio.
- On the File menu, point to New, and then select Project.
- Select Visual C# Projects under Project Types, and then select Console Application under Templates.
- Add the following code at the beginning of the Class1.cs file:
- Add the following code to the
Main
method: - On the Debug menu, select Start to compile and to run the application. Press ENTER to close the Console window. The Console window displays the contents of the Sample.txt file:
Write a text file (example 1)
The following code uses the
StreamWriter
class to open, to write, and to close the text file. In a similar way to the StreamReader
class, you can pass the path of a text file to the StreamWriter
constructor to open the file automatically. The WriteLine
method writes a complete line of text to the text file.- Start Visual Studio.
- On the File menu, point to New, and then select Project.
- Select Visual C# Projects under Project Types, and then select Console Application under Templates.
- Add the following code at the beginning of the Class1.cs file:
- Add the following code to the
Main
method: - On the Debug menu, select Start to compile and to run the application. This code creates a file that is named Test.txt on drive C. Open Test.txt in a text editor such as Notepad. Test.txt contains two lines of text:
Write a text file (example 2)
The following code uses the
StreamWriter
class to open, to write, and to close the text file. Unlike the previous example, this code passes two additional parameters to the constructor. The first parameter is the file path and the file name of the file. The second parameter, true
, specifies that the file is opened in append mode. If you specify false
for the second parameter, the contents of the file are overwritten each time you run the code. The third parameter specifies Unicode
, so that StreamWriter
encodes the file in Unicode format. You can also specify the following encoding methods for the third parameter:- ASC11
- Unicode
- UTF7
- UTF8
The
Write
method is similar to the WriteLine
method, except that the Write
method doesn't automatically embed a carriage return or line feed (CR/LF) character combination. It's useful when you want to write one character at a time.- Start Visual Studio.
- On the File menu, point to New, and then click Project.
- Click Visual C# Projects under Project Types, and then click Console Application under Templates.
- Add the following code at the beginning of the Class1.cs file:
- Add the following code to the
Main
method: - On the Debug menu, select Start to compile and to run the application. This code creates a file that is named Test1.txt on drive C. Open Test1.txt in a text editor such as Notepad. Test1.txt contains a single line of text: 0123456789.
Complete code listing for how to read a text file
Complete code listing for how to write a text file (version 1)
Complete code listing for how to write a text file (version 2)
Troubleshoot
For all file manipulations, it's good programming practice to wrap the code inside a
try-catch-finally
block to handle errors and exceptions. Specifically, you may want to release handles to the file in the final block so that the file isn't locked indefinitely. Some possible errors include a file that doesn't exist, or a file that is already in use.A fixed asset is written off when it is determined that there is no further use for the asset, or if the asset is sold off or otherwise disposed of. A write off involves removing all traces of the fixed asset from the balance sheet, so that the related fixed asset account and accumulated depreciation account are reduced.
Off The Record: Method Writing Example
There are two scenarios under which a fixed asset may be written off. The first situation arises when you are eliminating a fixed asset without receiving any payment in return. This is a common situation when a fixed asset is being scrapped because it is obsolete or no longer in use, and there is no resale market for it. In this case, reverse any accumulated depreciation and reverse the original asset cost. If the asset is fully depreciated, that is the extent of the entry.
Example of How to Write Off a Fixed Asset
For example, ABC Corporation buys a machine for $100,000 and recognizes $10,000 of depreciation per year over the following ten years. At that time, the machine is not only fully depreciated, but also ready for the scrap heap. ABC gives away the machine for free and records the following entry.
Debit | Credit |
Accumulated depreciation | 100,000 |
Machine asset | 100,000 |
![Off the record: method writing Off the record: method writing](/uploads/1/1/4/0/114014027/736856194.png)
A variation on this first situation is to write off a fixed asset that has not yet been completely depreciated. In this situation, write off the remaining undepreciated amount of the asset to a loss account. To use the same example, ABC Corporation gives away the machine after eight years, when it has not yet depreciated $20,000 of the asset's original $100,000 cost. In this case, ABC records the following entry:
Debit | Credit |
Loss on asset disposal | 20,000 |
Accumulated depreciation | 80,000 |
Machine asset | 100,000 |
The second scenario arises when you sell an asset, so that you receive cash (or some other asset) in exchange for the fixed asset you are selling. Depending upon the price paid and the remaining amount of depreciation that has not yet been charged to expense, this can result in either a gain or a loss on sale of the asset.
For example, ABC Corporation still disposes of its $100,000 machine, but does so after seven years, and sells it for $35,000 in cash. In this case, it has already recorded $70,000 of depreciation expense. The entry is:
Debit | Credit |
Cash | 35,000 |
Accumulated depreciation | 70,000 |
Gain on asset disposal | 5,000 |
Machine asset | 100,000 |
Off The Record: Method Writing Practice
What if ABC Corporation had sold the machine for $25,000 instead of $35,000? Then there would be a loss of $5,000 on the sale. The entry would be:
Debit | Credit |
Cash | 25,000 |
Accumulated depreciation | 70,000 |
Loss on asset disposal | 5,000 |
Machine asset | 100,000 |
Off The Record: Method Writing
Timing of Fixed Asset Write-Offs
A fixed asset write off transaction should only be recorded after written authorization concerning the targeted asset has been secured. This approval should come from the manager responsible for the asset, and sometimes also the chief financial officer.
Fixed asset write offs should be recorded as soon after the disposal of an asset as possible. Otherwise, the balance sheet will be overburdened with assets and accumulated depreciation that are no longer relevant. Also, if an asset is not written off, it is possible that depreciation will continue to be recognized, even though there is no asset remaining. To ensure a timely write off, include this step in the monthly closing procedure.
Related Courses
Off The Record: Method Writing Techniques
Fixed Asset Accounting
How to Audit Fixed Assets
How to Audit Fixed Assets