writing array to a file
writing array to a file
writing array to a file
writing array to a file
writing array to a file
writing array to a file writing array to a file writing array to a file writing array to a file writing array to a file writing array to a file writing array to a file writing array to a file
writing array to a file writing array to a file
writing array to a file
Go Back  Xtreme Visual Basic Talk > > > writing array to a file


Reply
 
Thread Tools Display Modes
  #1  
Old 05-31-2011, 08:11 PM
nik4537 nik4537 is offline
Newcomer
 
Join Date: May 2011
Posts: 1
Default writing array to a file


I hope someone can help me with this...
I am trying to print the contents of my array to a text file, but instead of just printing whats in the array it prints all indexes even if they are empty.

this is the code i am using....any help would be appreciated. Thanks
Code:
  FileOpen(3, "garbage.txt", OpenMode.Append)
        Dim intCounter As Int16
        For intCounter = strFirstName.GetLowerBound(0) To strFirstName.GetUpperBound(0)
            WriteLine(3, strFirstName(intCounter), strMiddleInitial(intCounter), strLastName(intCounter), _
                      strCreditCard(intCounter), strExpiryDate(intCounter), strSecurityCode(intCounter), _
                      strProsthetic(intCounter), strCommissionDate(intCounter), strCompletionDate(intCounter), _
                      blnFemale(intCounter), blnMale(intCounter), intManufactureTime(intCounter), blnCarbon(intCounter), _
                      blnSkin(intCounter), blnReflex(intCounter), blnEnvision(intCounter), blnWiFi(intCounter))
        Next

        FileClose(3)

Last edited by Flyguy; 06-01-2011 at 02:04 AM. Reason: Added [code][/code] tags
Reply With Quote
  #2  
Old 06-01-2011, 04:18 AM
KingOftoasty KingOftoasty is offline
Newcomer
 
Join Date: May 2010
Posts: 3
Default

I could be wrong on this, but I think instead of separating the pieces of the line to be written with the "," character, that you need to use the "&" Character. Don't know if that's whats actually causing the problem though. You might also convert them to strings with the CStr operator before actually writing them.

Also, instead of using the old file.open command, you might look into using the StreamWriter method.

Hope this helps
Reply With Quote
  #3  
Old 06-01-2011, 09:20 AM
AtmaWeapon's Avatar
AtmaWeaponwriting array to a file AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

As KingOftoasty pointed out, FileOpen and its ilk are compatibility crutches for VB6 programmers. It's often easier and more clear to use the object-oriented .NET methods from the System.IO namespace.

File.AppendText() opens a file for writing and will append data to the end. You can create a StreamWriter based on that to write the text to the file; from there, you write text using the WriteLine() or Write() methods. For example:
Code:
Dim outputFile As FileStream = File.AppendText("garbage.txt")
Using writer As New StreamWriter(outputFile)
    For index As Integer = 0 To firstNames.Length - 1
        writer.WriteLine(firstNames(index))
        writer.WriteLine(middleInitial(index))
        ...
    Next
End Using

outputFile.Dispose()
I'd like to discuss this in detail, but first can you confirm you're using VB .NET? If you're using VB6 my advice will be useless.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
writing array to a file
writing array to a file
writing array to a file writing array to a file
writing array to a file
writing array to a file
writing array to a file writing array to a file writing array to a file writing array to a file writing array to a file writing array to a file writing array to a file
writing array to a file
writing array to a file
 
writing array to a file
writing array to a file
 
-->