?
Solved

Remove Last Return Character in Text File

Posted on 2005-03-30
6
Medium Priority
?
1,113 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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!

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

764 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