LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-04-2005, 11:31 AM   #1
mrobertson
Member
 
Registered: May 2005
Posts: 275

Rep: Reputation: 30
Writing to a text file vb.net


I am currently usin the following line of code to write to a text file:

Code:
stw.WriteLine(txtgradecode.Text & "," & txtwidth.Text & "," & txthbgauge.Text & "," & txttargetgauge.Text)
When I view the text file, the text is written on the 2nd line with the cursor on the first line(a blank line). Later when I try to read my text file, I am getting an error because it is trying to read a blank line . Is there a way around this problem?
 
Old 08-04-2005, 11:44 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
WriteLine is going to always put a carriage return/linefeed pair at the end of whatever you output. That is why it is called WriteLine.

You could use just Write, but then you'll have to make sure you add your own CRLF when you want to start a new line. A better solution would probably be to check the length of the lines you read in, and if the length is 0, ignore it...
 
Old 08-04-2005, 01:07 PM   #3
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
How would you ignore it and tell the reader to move to the next line?
 
Old 08-04-2005, 01:23 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
By using a simple if statement.

Code:
while ((sReadLine = file.ReadLine()) != null)
{
    if (sReadLine.Length > 0)
    {
        // do your thing
    }
}
BTW, I hope all these VB .net questions aren't homework related. You won't learn much if you don't try to think though the problems a bit on your own.
 
Old 08-04-2005, 01:28 PM   #5
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Also, I re-read the first post and realized you said there was a blank line BEFORE your line. Are you sure you don't use a WriteLine anywhere else? Are you opening the file for append or overwrite? If append, was there previously a blank line in the file? What are you using to view the file?

Ignoring the blank lines should still be done as you'll likely have a blank line at the end of the file, but it might be good for you to understand where the other blank lines are coming from as well.
 
Old 08-04-2005, 01:37 PM   #6
mrobertson
Member
 
Registered: May 2005
Posts: 275

Original Poster
Rep: Reputation: 30
That worked!

Thanks alot
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 12:28 PM
vb.net reading and writing to a text file simultaneously mrobertson Programming 3 09-08-2005 01:30 PM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 08:15 PM
Text editor for writing bgfay Linux - Software 3 07-19-2004 02:32 AM
java - Writing to a text area Risc91 Programming 6 05-29-2004 01:58 PM


All times are GMT -5. The time now is 10:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration