?
Solved

Remove Last Return Character in Text File

Posted on 2005-03-30
6
Medium Priority
?
1,117 Views
Last Modified: 2010-04-24
Experts,

I have a program that writes data to a text file using StreamWriter.WriteLine(). It works great but the last WriteLine I do Leaves a Return Charater at the end of the line forcing the file to the next line. The file cannont have a blank line at the end. I need a function that will remove only the last Return Character so it does not have a blank line at the end of the file.

Thanks,

Bob
0
Comment
Question by:bobbailey22
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:Lacutah
ID: 13667379
When you're at the last line, instead of using StreamWriter.WriteLine, use StreamWriter.Write
0
 

Author Comment

by:bobbailey22
ID: 13667472
That would work but I won't know when I'll be at the last line. The StreamWriter.WriteLine is in a for loop.
0
 
LVL 9

Expert Comment

by:Lacutah
ID: 13667487
That's OK, modify the "for" loop so that the first thing it does is write a line, for example:

dim writeString as string
dim isFirst as boolean = True
dim s as string ' for the example
for each s in MyStrings() 'Whatever you're looping through
       'The following line prevents writing a line on the first iteration
       if not IsFirst then StreamWriter.WriteLine(writeString)
       IsFirst = False
       writeString = s
Next
'Now, the last string to be written...
StreamWriter.Write(writeString)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Accepted Solution

by:
prakash_prk earned 1050 total points
ID: 13668864
For directly write into the file.
first write the contents into a string variable.
Then finally write the variable to the file

dim svlFileContent as string

'some loop
svlFileContent = "Content To Write" & Microsoft.VisualBasic.vbCrLf
'end loop

'Remove the last charactor.
svlFileContent = Microsoft.VisualBasic.Left(svlFileContent, svlFileContent.Length - 2)
StreamWriter.Write(svlFileContent)
0
 
LVL 9

Assisted Solution

by:Lacutah
Lacutah earned 450 total points
ID: 13677706
prakash_prk, your answer is concise, but should be understood that this could consume vast amounts of memory and be much slower when writing more than 100 or so lines.  Also, since I assume you meant to append each line
(your
    svlFileContent = "Content To Write" & Microsoft.VisualBasic.vbCrLf
should read
    svlFileContent &= "Content To Write" & Microsoft.VisualBasic.vbCrLf
) unfortunately, again, with a large amount of data this gets VERY memory intensive, since each iteration creates a new string, and GC has to play catch up to rescue freed resources.  For example, say I have a string that is 1 MB long, and want to append (using your method) another string of 1KB.  To do this the CLR creates a NEW string of 1MB + 1KB - allocating seperate memory space.  Strings are not "appendable" objects - they are immutable, once initialized, cannot be changed, so the example operation would eat up 2MB + 1KB of RAM.  This is why we use StringBuilders when lots of appending is involved.
0
 
LVL 10

Expert Comment

by:prakash_prk
ID: 13678099
Yeah Lacutah. Thank  u for I was used lot of programs like that . Thank u for your suggestion . I will change my programs.

thank u
prakash
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question